// 设置颜色根据范围
- (void)addColor:(UIColor *)color location:(NSInteger)location length:(NSInteger)length
{
NSMutableAttributedString *attStr = [self.attributedText mutableCopy];
if (location != NSNotFound) {
[attStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(location, length)];
}
self.attributedText = attStr;
}
// 设置字体根据范围
- (void)addFont:(CGFloat)size location:(NSInteger)location length:(NSInteger)length
{
NSMutableAttributedString *attStr = [self.attributedText mutableCopy];
if (location != NSNotFound) {
[attStr addAttribute:NSFontAttributeName value:FontWithWidth(size) range:NSMakeRange(location, length)];
}
self.attributedText = attStr;
}
// 设置行间距
- (void)addParagraphStyle:(CGFloat)size
{
NSMutableAttributedString *attStr = [self.attributedText mutableCopy];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setLineSpacing:size];
[attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.text.length)];
self.attributedText = attStr;
}
给UILabel扩展以上方法就可以了,如有bug欢迎联系,会修复。