美文网首页
UIlabel 中利用NSParagraphStyle设置行距,

UIlabel 中利用NSParagraphStyle设置行距,

作者: Heikki_ | 来源:发表于2020-05-14 15:45 被阅读0次

抄自:http://www.star-lai.cn/1443.html

UIlabel 中利用NSParagraphStyle设置行距,只有一行中文的时候底下会留空白;

 NSString *contentStr=@"试试看";
 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr];
 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
 paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
 [paragraphStyle setLineSpacing:5];
 [_contentLabel setAttributedText:attributedString];
得到效果: image

只有一行中文的时候底下会留空白(其实是lineSpacing高度),当字数大于两行时候底下又没有空白。

解决办法:

设置NSBaselineOffsetAttributeName为0;

[attributedString addAttribute:NSBaselineOffsetAttributeName value:@(0) range:NSMakeRange(0, [contentStr length])];

相关文章

网友评论

      本文标题:UIlabel 中利用NSParagraphStyle设置行距,

      本文链接:https://www.haomeiwen.com/subject/bswtohtx.html