有的时候我们想一个label的text中出现不同的颜色,而且label出现的地方也很多,所以就自己写了个复用的UILabel的扩展。
以下是我自己写的:
<pre>
@implementation UILabel(MoreColorsText)
-(void)setAttributeLabelWithtextColorPairs:(NSArray*)colorTextPairs
{
//获取Label的带属性字符串
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
NSString *text = self.text;
NSRange oriRange = NSMakeRange(0, 0), changedRange = NSMakeRange(0, 0);
for (NSDictionary *textColorDic in colorTextPairs) {
//计算要修改的textRange
NSRange textRange = [text rangeOfString:textColorDic.allKeys.firstObject];
NSUInteger location = (oriRange.location + oriRange.length) + textRange.location;
changedRange = NSMakeRange(location, textRange.length);
//修改指定范围的textColor
UIColor *curColor = nil;
if ([textColorDic.allValues.firstObject isKindOfClass:[UIColor class]]) {
curColor = textColorDic.allValues.firstObject;
}else{
curColor = [UIColor colorWithHexString:textColorDic.allValues.firstObject];
}
[attrStr addAttribute:NSForegroundColorAttributeName value:curColor range:changedRange];
//将修改的range改为旧的Range
oriRange = changedRange;
//从匹配字符串的结尾开始截取剩下的字符串
if (textRange.location != NSNotFound &&
self.text.length > (textRange.location + textRange.length)) {
text = [text substringFromIndex:textRange.location + textRange.length];
}
}
//将修改好的AttributedString赋值给Label
self.attributedText = attrStr;
}
@end
</pre>
使用方法:
<pre>
//label的默认text=@"123456789"
[self.label setAttributeLabelWithtextColorPairs:@[@{@"2":@"FF7200"},
@{@"7":[UIColor redColor]}]];
</pre>
以上代码肯定会有不完善的地方欢迎大家完善和补充:
例子下载地址