【iOS】设置同一个Label展示不同颜色,字体

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 120, 30)]; 
label.font = [UIFont systemFontOfSize:16.0f]; 
label.textColor = [UIColor blackColor];
label.numberOfLines = 0;  
// 遇到\n自动换行
label.lineBreakMode = NSLineBreakByWordWrapping;
[self.view addSubview:label];  
          
NSMutableAttributedString *textStr = [[NSMutableAttributedString alloc]initWithString:@"张三 已预约"];  
// 获取要调整文字样式的位置 
NSRange range = [[textStr string]rangeOfString:@"张三"];  
// value: 就是要改变的label属性(颜色、字体等)
// 设置不同字体
// [textStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20.0f] range:range];  
// 设置不同颜色
[textStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];  
                    
label.attributedText = textStr;  

你可能感兴趣的:(【iOS】设置同一个Label展示不同颜色,字体)