项目中需要实现这么一条Label
//金额标红
//先将字符串以,隔开
NSArray *totalMoenyArray = [totalMoneyText componentsSeparatedByString:@","];
NSInteger moneyLastLength = 0;
//如果隔开后有两个或以上字符串
if (totalMoenyArray.count>1) {
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:totalMoneyText];
for(int i=0;i<totalMoenyArray.count;i++){
NSString *moneyStr= totalMoenyArray[i];
NSInteger length = moneyStr.length;
NSString *moneyLastStr = @"";
if (i>0) {
moneyLastStr = totalMoenyArray[i-1];
moneyLastLength = moneyLastStr.length + moneyLastLength;
}
[str addAttribute:NSForegroundColorAttributeName value:RGB(226, 58, 58)range:NSMakeRange(3+moneyLastLength+i,length-4)];
}
self.totalMoneyLable.attributedText = str;
}else{
NSMutableAttributedString *countAttributeString = [[NSMutableAttributedString alloc]initWithString:totalMoneyText];
[countAttributeString addAttributes:@{NSForegroundColorAttributeName:RGB(226, 58, 58)} range:NSMakeRange(3, totalMoneyText.length-4)];
self.totalMoneyLable.attributedText = countAttributeString;
}
//属性参数
NSFontAttributeName //字体 默认是12号
NSParagraphStyleAttribut
NSForegroundColorAttribu
NSBackgroundColorAttribu
NSLigatureAttributeName//连体字符
NSKernAttributeName//字距的像素
NSStrikethroughStyleAttr
NSUnderlineStyleAttribut
NSStrokeColorAttributeNa
NSStrokeWidthAttributeNa
NSShadowAttributeName//阴影
NSUnderlineColorAttribut