关于修改NSString中指定字符串的属性

NSString *tipsStr  = @"假如你有一段很长很长的字符串,然后我需要修改其中几个字的颜色:[这几个字包括括号的颜色需要被修改],后面还接着很多字的,而且字体颜色和中括号中的不一样,但是和前面的字体颜色又是一样的";
NSMutableAttributedString *colorStr = [[NSMutableAttributedString alloc] initWithString:tipsStr];
[colorStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:13*1.0/255 green:136*1.0/255 blue:232*1.0/255 alpha:1] range:NSMakeRange(32, 18)];
[colorStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:159*1.0/255 green:162*1.0/255 blue:167*1.0/255 alpha:1] range:NSMakeRange(0, 32)];
[colorStr addAttribute:NSForegroundColorAttributeName value:``[UIColor colorWithRed:159*1.0/255 green:162*1.0/255 blue:167*1.0/255 alpha:1] range:NSMakeRange(50, tipsStr.length-50)];
UITextView *textView = [[UITextView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:textView];
textView.attributedText = colorStr;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容