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)里面的参数也需要做相应的调整!

你可能感兴趣的:(iOS设定字符串某段的颜色&字号)