iOS设定字符串某段的颜色&字号

例如:累计销售100

上面的100设置为红色,该怎么处理

self.salesLabel.attributedText=[self  changedTextColor:totalStrlength4orgPrice:model.total.length];


-(NSMutableAttributedString*)changedTextColor:(NSString*)orgStr length4orgPrice:(NSInteger)length4orgPrice

{

NSMutableAttributedString*mutableString = [[NSMutableAttributedString alloc]initWithString:orgStr];

//给某一范围的字符设置特定颜色

[mutableString addAttribute:NSForegroundColorAttributeName  value:[UIColorblueColor]  range:NSMakeRange(4, length4orgPrice)];

//给某一范围的字符设置更大的字号

[mutableStringaddAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:16]range:NSMakeRange(2, length4orgPrice)];

return  mutableString;

}

如果一串字符串 需要多处设置字体颜色  只需要在上面的方法体里面多增加方法

[mutableString addAttribute:NSForegroundColorAttributeName  value:[UIColorblueColor]  range:NSMakeRange(4, length4orgPrice)];

当然NSMakeRange(4, length4orgPrice)里面的参数也需要做相应的调整!

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

推荐阅读更多精彩内容