调整label的行间距、字间距
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调整label的行间距、字间距相关的知识,希望对你有一定的参考价值。
参考技术A //改变label的行间距- (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:label.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:space];
[attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];
label.attributedText = attributedStr;
[label sizeToFit];
//改变label的字间距
- (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text attributes:@NSKernAttributeName:@(space)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];
label.attributedText = attributedString;
[label sizeToFit];
//改变行间距和字间距
- (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text attributes:@NSKernAttributeName:@(wordSpace)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpace];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];
label.attributedText = attributedString;
[label sizeToFit];
以上是关于调整label的行间距、字间距的主要内容,如果未能解决你的问题,请参考以下文章
NSMutableAttributedString怎么设置label的行间距