UILabel设置多种字体、颜色

来源:互联网 发布:个性二维码生成软件 编辑:程序博客网 时间:2024/06/11 15:11
  1. NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String,try your best to test attributed string text"];  
  2.  [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];  
  3.  [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];  
  4.  [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];  
  5.  [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:30.0] range:NSMakeRange(05)];  
  6.  [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:30.0] range:NSMakeRange(612)];  
  7.  [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:30.0] range:NSMakeRange(196)];  
  8.    
  9.  UILabel *attrLabel = [[UILabel alloc] initWithFrame:CGRectMake(20150320 - 4090)];  
  10.  attrLabel.attributedText = str;  
  11.  attrLabel.numberOfLines = 0;  
  12.  [self.view addSubview:attrLabel]; 
0 0