NSMutableAttributeString不同颜色的label

NSString*reslutStr =@"27十二月";

NSMutableAttributedString*attriString = [[NSMutableAttributedStringalloc]initWithString:reslutStr];

[attriStringaddAttribute:NSForegroundColorAttributeName//文字颜色

value:[UIColororangeColor]

range:NSMakeRange(2,reslutStr.length-2)];

cell.timeLabel.attributedText= attriString;

+ (void)setUILabel:(UILabel*)label Data:(NSString*)string SetData:(NSString*)setstring Color:(UIColor*)color Font:(CGFloat)font Underline:(BOOL)isbool{

NSRangestr = [label.textrangeOfString:setstring];

if(str.location!=NSNotFound) {

NSMutableAttributedString*str1 = [[NSMutableAttributedStringalloc]initWithString:label.text];

[str1addAttribute:NSForegroundColorAttributeNamevalue:colorrange:NSMakeRange(string.length,setstring.length)];//设置字体颜色

[str1addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:font]range:NSMakeRange(string.length,setstring.length)];

if(isbool) {

//是否显示下划线

[str1addAttribute:NSStrikethroughStyleAttributeNamevalue:@(NSUnderlinePatternSolid|NSUnderlineStyleSingle)range:NSMakeRange(string.length,setstring.length)];

}

label.attributedText= str1;

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容