iOS开发 UILabel 设置行间距,以及居中对齐
2022-05-05 本文已影响0人
我是卖报的小行家
解决UILabel设置富文本字体后(即设置行间距),文字没有居中显示
label.textAlignment = NSTextAlignmentCenter; //只设置这句是无效的
NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//关键代码
paragraphStyle.alignment = NSTextAlignmentCenter;
[paragraphStyle setLineSpacing:5];//设置距离
[attriString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [text length])];
label.attributedText = attriString;