首页 > 代码库 > UILabel 调整行间距
UILabel 调整行间距
/* 调整行间距 */ + (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; }
UILabel 调整行间距
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。