/*
调整行间距
*/
+ (void)adjustLineSpacingOfLabel:(UILabel *)label to:(CGFloat)lineSpacing
{
NSString *textStr = label.text;
if (textStr.length <= 0) {
return;
}
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textStr];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpacing];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [textStr length])];
label.attributedText = attributedString;
[label sizeToFit];
}
+ (CGSize)sizeOfMultiLineLabel:(NSString *)aLabelTextString font:(UIFont *)aLabelFont constrainedToSize:(CGSize)maxSize lineSpacing:(CGFloat)lineSpacing
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) {
//version < 7.0
return [aLabelTextString sizeWithFont:aLabelFont
constrainedToSize:maxSize
lineBreakMode:NSLineBreakByTruncatingTail];
}
else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
//version >= 7.0
//Return the calculated size of the Label
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpacing];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
return [aLabelTextString boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:aLabelFont, NSParagraphStyleAttributeName:paragraphStyle}
context:nil].size;
}
return CGSizeZero;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步