UILabel实现文字缩进

NSMutableAttributedString *text2 = [[NSMutableAttributedString alloc] initWithString:model.reply];
    //设置缩进、行距
    NSMutableParagraphStyle *style2 = [[NSMutableParagraphStyle alloc] init];
// 对齐方式
    style2.alignment = NSTextAlignmentJustified;
// 头部缩进
    style2.headIndent = 10.0f;
// 首行缩进
    style2.firstLineHeadIndent = 10.0f;
// 尾部缩进
    style2.tailIndent = -10.0f;
    [text2 addAttribute:NSParagraphStyleAttributeName value:style2 range:NSMakeRange(0, model.reply.length)];
    self.remark.attributedText = text2;

懒加载remark

-(UILabel *)remark{
    if (_remark == nil) {
        _remark = [[UILabel alloc] init];
        _remark.font = FONT(FONT_BIG_SIZE);
        _remark.textColor = C_MAIN_COLOR;
        _remark.textAlignment = NSTextAlignmentJustified;
        _remark.layer.masksToBounds = YES;
        _remark.layer.cornerRadius = 5.f;
        _remark.layer.borderColor = C_MAIN_COLOR.CGColor;
        _remark.layer.borderWidth = 1.f;
        _remark.backgroundColor = [C_MAIN_COLOR colorWithAlphaComponent:0.2f];
        
    }
    return _remark;
}


效果如下图(其中remark事先设置了边框和背景,还进行高度自适应)

UILabel实现文字缩进_第1张图片
IMG_3216.PNG

你可能感兴趣的:(UILabel实现文字缩进)