根据范围给UILabel设置颜色或字体,行间距

来源:互联网 发布:淘宝上最火的小吃店铺 编辑:程序博客网 时间:2024/06/04 01:36

// 设置颜色根据范围
- (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欢迎联系,会修复。

0 0
原创粉丝点击