您的位置:首页 > 其它

label的富文本设置

2016-07-08 22:40 447 查看
/**传入一个label,范围字符串的字体颜色为自定义的颜色*/
- (NSMutableAttributedString*)setMutableAttributesStringWithString:(UILabel *)label range:(NSRange)range
{
    //范围字符串属性修改
    NSDictionary *attributes =
@{NSForegroundColorAttributeName:label.textColor
                                 };
    //1.范围截取字符串
    NSString *rangeString = [label.text
substringWithRange:range];
    //2.将字符串改为可变属性字符串
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc]initWithString:label.text
attributes:attributes];
    //3.设置属性和需要修改的文字范围
    UIColor *black = [UIColor
blackColor];
    NSRange redTextRange = [label.text
rangeOfString:rangeString];
    //4.赋值给可变属性字符串
    [attributedString setAttributes:@{NSForegroundColorAttributeName:black}
range:redTextRange];
    return attributedString;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: