用label显示多行文字的时候,常面临设置各种样式的情况,这里简单记录下刚做的需求
- 首先创建懒加载的形式创建label
- (UILabel *)licenseTextLabel{
if (!_licenseTextLabel) {
_licenseTextLabel = [UILabel new];
_licenseTextLabel.textColor = ZSAppColor.primary303030;
_licenseTextLabel.font = ZSAppFont.system15;
_licenseTextLabel.numberOfLines = 0;
}
return _licenseTextLabel;
}
- 设置具体样式参数并赋值
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = NSTextAlignmentLeft; //对齐
style.headIndent = 0.0f;//行首缩进
CGFloat emptylen = self.licenseTextLabel.font.pointSize * 2;//参数:(字体大小号字乘以2,即首行空出两个字符)
style.firstLineHeadIndent = emptylen;//首行缩进
style.tailIndent = 0.0f;//行尾缩进
style.lineSpacing = 4.0f;//行间距
NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:@"文字描述" attributes:@{NSParagraphStyleAttributeName: style}];
self.licenseTextLabel.attributedText = attrText;
网友评论