UILabel文字缩进

  ↵  

使用方法:

NSString * str = @"故障现象:传感器1-乱跳,传感器2-乱跳,传感器3-乱跳,传感器4-乱跳";
    NSMutableAttributedString * muStr = [[NSMutableAttributedString alloc] initWithString:str];
    NSDictionary *valueColorAttrs = @{ NSForegroundColorAttributeName :valueColor ? : [UIColor dk_colorWithHexString:@"#7F7F7F"]};
    NSRange range = NSMakeRange(title.length+1, str.length-title.length-1);
    [muStr addAttributes:valueColorAttrs range:range];
    
    //设置缩进、行距
    NSMutableParagraphStyle *style2 = [[NSMutableParagraphStyle alloc] init];
    // 对齐方式
    style2.alignment = NSTextAlignmentLeft;
    // 除第一行以为行的缩进
    style2.headIndent = 65.0f;
    [muStr addAttribute:NSParagraphStyleAttributeName value:style2 range:NSMakeRange(0, str.length)];
    
    NSAttributedString * attributeString = [[NSAttributedString alloc] initWithAttributedString:muStr];
    UILabel * label = [[UILabel alloc]init];
    label.attributeText = attributeString;
     

效果如下:

 

猜你喜欢

转载自blog.csdn.net/humiaor/article/details/86311199