label的富文本设置

/**传入一个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;

}

你可能感兴趣的:(label的富文本设置)