字符串中改变任意字的颜色和字体大小

来源:互联网 发布:二端电阻网络的无源性 编辑:程序博客网 时间:2024/06/07 01:05

字符串中改变任意字的颜色和字体大小
UILabel * showLab=[[UILabel alloc] initWithFrame:CGRectMake(5, 100, self.view.bounds.size.width-10, 50)];
showLab.text=@”简介:hello world你好”;
[self.view addSubview:showLab];
NSRange range = [showLab.text rangeOfString:@”简介”];
NSRange moneyRange=[showLab.text rangeOfString:@”你好”];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:showLab.text];
//设置:在传过来的的内容显示成红色
[str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:39/255.0 green:141/255.0 blue:41/255.0 alpha:1] range:range];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:39/255.0 green:141/255.0 blue:41/255.0 alpha:1] range:moneyRange];
[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:range];
showLab.attributedText = str;
喜欢请加群:526272260 这里写图片描述

0 0