实现一个字符串中的文字显示多种颜色

来源:互联网 发布:猎豹高清网络电视apk 编辑:程序博客网 时间:2024/06/05 01:19

-(void)viewWillAppear:(BOOL)animated

{

    [superviewWillAppear:animated];

    NSString * str1=@"我要改变文字的颜色";

    NSString * str2=@"红色的文字";

    

    NSDictionary * dic=@{NSForegroundColorAttributeName:[UIColorredColor]};

    NSAttributedString * att=[[NSAttributedStringalloc]initWithString:str2attributes:dic];

    

    NSMutableAttributedString * attstr=[[NSMutableAttributedStringalloc]initWithString:str1];

    [attstr appendAttributedString:att];

    

    self.lable.attributedText=attstr;

    

}


效果如下图:

0 0