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;
}
时间: 2024-12-15 14:42:09

UILabel 调整行间距的相关文章

UILabel 调整行间距计算frame

+ (UILabel *)getLabelFrame:(UILabel *)label withLineSpace:(NSInteger)lineSpace withAttributedString:(NSString *)string { [label setNumberOfLines:0]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:strin

UILabel的行间距

一提到UILabel的行间距,一般都是用下面的方法解决. UILabel *label = [[UILabel alloc] initWithFrame:labelFrame]; [label setBackgroundColor:[UIColor clearColor]]; [label setTextColor:textColor]; [label setNumberOfLines:0]; label.font = textFont; NSString *labelText = @"我是内容

iOS UILabel的行间距

今天美工要求label的字体之间要有一定的间距,我的数据由于是网络请求下来的没有设置成功,只能调整段落之间的间距, UILabel设置行间距等属性: NSMutableAttributedString *attributedString =[[NSMutableAttributedStringalloc]initWithString:contentLabel.text];; NSMutableParagraphStyle*paragraphStyle = [[NSMutableParagraph

iOS UILabel文本行间距

一提到UILabel的行间距,一般都是用下面的方法解决. UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];         [label setBackgroundColor:[UIColor clearColor]];         [label setTextColor:textColor];         [label setNumberOfLines:0];         label.font = textFo

UILabel设置行间距和字间距并计算高度-b

#define UILABEL_LINE_SPACE 6 #define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height //给UILabel设置行间距和字间距 -(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font { NSMutableParagraphStyle *paraStyle = [[NSMutablePara

(转载)iOS UILabel自定义行间距时获取高度

本文介绍一下自定义行间距的UILabel的高度如何获取,需要借助一下开源的UILabel控件:TTTAttributedLabel 附下载地址 https://github.com/TTTAttributedLabel/TTTAttributedLabel 下载后,添加到工程里面,导入头文件 #import "TTTAttributedLabel.h" 直接上代码 NSString *str = @"UILabel自定义行间距时获取高度,UILabel自定义行间距时获取高度,

UILabel设置行间距

UILabel设置行间距: NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:contentLabel.text]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc]init]; [paragraphStyle setLineSpacing:5]; [attri

iOS UILabel自定义行间距时获取高度

本文介绍一下自定义行间距的UILabel的高度如何获取,需要借助一下开源的UILabel控件:TTTAttributedLabel 附下载地址 https://github.com/TTTAttributedLabel/TTTAttributedLabel 下载后,添加到工程里面,导入头文件 #import "TTTAttributedLabel.h" 直接上代码 NSString *str = @"UILabel自定义行间距时获取高度,UILabel自定义行间距时获取高度,

ios 设置 UILabel的行间距

//行高 #define kLineSpace 1.5 self.memo.text = self.ticketModel.memo; //设置字体大小 self.memo.font = kFontSize(12); //计算字体size CGSize memoSize =  [self.memo.textsizeWithFont:kFontSize(12) maxSize:CGSizeMake(260, MAXFLOAT)]; //计算加入行高后文字的高度 CGFloat linesH = (