实现标题不缩进,所有内容缩进

image.png


NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
// 对齐方式
style.alignment = NSTextAlignmentJustified;
// 首行缩进
style.firstLineHeadIndent = 0.0f;
style.paragraphSpacing = 2;
// 头部缩进
style.headIndent = 40.0f;
// 尾部缩进
style.tailIndent = -40.0f;
NSString *title = @"第一.";
NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:title attributes:@{ NSParagraphStyleAttributeName : style}];
UILabel *tool = [UILabel new];
tool.attributedText = attrText;
[tool sizeToFit];
CGFloat head = CGRectGetMaxX(tool.frame) + 0;
NSLog(@"%f",head); // 49.666667 39.666667
title = @"利用 NSMutableParagraphStyle 实现文字缩进,利用 NSMutableParagraphStyle 实现文字缩进,利用 NSMutableParagraphStyle 实现文字缩进,利用 NSMutableParagraphStyle 实现文字缩进,利用 NSMutableParagraphStyle 实现文字缩进,利用 NSMutableParagraphStyle 实现文字缩进"
@"利用 NSMutableParagraphStyle 实现文字缩进";
style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
// 对齐方式
style.alignment = NSTextAlignmentJustified;
// 首行缩进
style.firstLineHeadIndent = head;
// 头部缩进
style.headIndent = head;
NSAttributedString *contentAttrText = [[NSAttributedString alloc] initWithString:title attributes:@{ NSParagraphStyleAttributeName : style}];
[attrText appendAttributedString:contentAttrText];
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.numberOfLines = 0;
label.attributedText = attrText;
label.backgroundColor = [UIColor yellowColor];
[self.view addSubview:label];

你可能感兴趣的:(实现标题不缩进,所有内容缩进)