UILabel和UIbutton(富文本)封装方法

/**
 方法说明:设置label的富文本属性
 参数说明:contentStr富文本内容
        textColor字体颜色
        rangeSet设置字体颜色及大小的位置
 */
- (UILabel *)backfwbLabelWithText:(NSString *)contentStr textColor:(NSString *)textColor rangeSet:(NSInteger)rangeSet
{
    // 文本信息
    NSString *str = contentStr;
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
    // attrStr添加字体和设置字体的范围
    [attrStr addAttribute:NSFontAttributeName
                    value:[UIFont systemFontOfSize:14.0f]
                    range:NSMakeRange(0, rangeSet)];
    // attrStr添加文字颜色
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:getColor(textColor)
                    range:NSMakeRange(0, rangeSet)];
    // 段落样式
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    //行间距
    paragraph.lineSpacing = 5;
    //段落间距
    paragraph.paragraphSpacing = 0;
    //对齐方式
    paragraph.alignment = NSTextAlignmentLeft;
    //指定段落开始的缩进像素
    paragraph.firstLineHeadIndent = 0;
    //调整全部文字的缩进像素
    paragraph.headIndent = 0;
    
    [attrStr addAttribute:NSParagraphStyleAttributeName
                    value:paragraph
                    range:NSMakeRange(0, [str length])];
    // 添加Label
    UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    firstLabel.backgroundColor = getColor(whiteColor);
    firstLabel.font = DEF_FontSize_10;
    //自动换行
    firstLabel.numberOfLines = 0;
    //设置label的富文本
    firstLabel.attributedText = attrStr;
    //label高度自适应
    [firstLabel sizeToFit];
    
    return firstLabel;
}
- (UIButton *)yhxyBtn
{
    if (!_yhxyBtn) {
        // 文本信息
        NSString *str = @"登录代表你已同意《非定不可用户协议》";
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
        // attrStr添加字体和设置字体的范围
        [attrStr addAttribute:NSFontAttributeName
                        value:[UIFont systemFontOfSize:13.0f]
                        range:NSMakeRange(0, 18)];
        // attrStr添加文字颜色
        [attrStr addAttribute:NSForegroundColorAttributeName
                        value:getColor(mainColor)
                        range:NSMakeRange(8, 10)];
        // 段落样式
        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        //行间距
        paragraph.lineSpacing = 5;
        //段落间距
        paragraph.paragraphSpacing = 0;
        //对齐方式
        paragraph.alignment = NSTextAlignmentLeft;
        //指定段落开始的缩进像素
        paragraph.firstLineHeadIndent = 0;
        //调整全部文字的缩进像素
        paragraph.headIndent = 0;
        
        [attrStr addAttribute:NSParagraphStyleAttributeName
                        value:paragraph
                        range:NSMakeRange(0, [str length])];
        _yhxyBtn = [[UIButton alloc]init];
        [_yhxyBtn setBackgroundColor:[UIColor clearColor]];
        //设置label的富文本
        [_yhxyBtn setAttributedTitle:attrStr forState:UIControlStateNormal];
        [_yhxyBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
    }
    return _yhxyBtn;
}
原文地址:https://www.cnblogs.com/dujiahong/p/8266798.html