iOS字符串显示不同颜色

字体大小一样

NSString *heightlightString = @"叫我第一名";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:heightlightString];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)];

label.attributedText = str;

字体大小不一样

NSString *string = [NSString stringWithFormat:@"联系电话:12345678899"];
NSMutableAttributedString *myString = [[NSMutableAttributedString alloc] initWithString:string];
[myString addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(5, 11)]; 
    clockLabel.attributedText = stra;

label.attributedText = myString;

range需要location和length两个参数,设置想要改动的字体颜色和范围.

你可能感兴趣的:(iOS字符串显示不同颜色)