您的位置:首页 > 移动开发 > IOS开发

iOS为一段文字显示不同的颜色

2016-11-11 16:12 453 查看
//需求:0次为红色

   
NSString
*str =
@"您有0次拆红包的机会";

   
NSMutableAttributedString
*attributedStr = [[NSMutableAttributedString

alloc]initWithString:str];

   
UIColor
*color = [AppUtils

callColorFromHexRGB:@"5f5f5f"];

    [attributedStr
addAttribute:NSForegroundColorAttributeName

                         
value:color

                         
range:[str

rangeOfString:@"您有

次拆红包的机会"]];

   
UIColor
*color1 = [UIColor

redColor];

    [attributedStr
addAttribute:NSForegroundColorAttributeName

                         
value:color1

                         
range:[str

rangeOfString:@"0"]];

   

  

//设置label的行间距
 NSMutableParagraphStyle
*paregraphStyle = [[NSMutableParagraphStyle

alloc]

                                              
init];

    [paregraphStyle
setLineSpacing:10.0];
    [attributedStr
addAttribute:NSParagraphStyleAttributeName
value:paregraphStyle
range:NSMakeRange(0,
str.length)];

   

hLabel
= [[UILabel

alloc]init];
   
hLabel.attributedText
= attributedStr;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: