美文网首页
带有行间距文字的label 取高度

带有行间距文字的label 取高度

作者: 奕珃 | 来源:发表于2017-08-21 14:17 被阅读0次

    #define RowSpacing 5

    #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

    //给UILabel设置行间距和字间距

    -(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {

    NSMutableParagraphStyle*paraStyle = [[NSMutableParagraphStyle alloc] init];

    paraStyle.lineBreakMode=NSLineBreakByCharWrapping;

    paraStyle.alignment=NSTextAlignmentLeft;

    paraStyle.lineSpacing= RowSpacing; //设置行间距

    paraStyle.hyphenationFactor=1.0;

    paraStyle.firstLineHeadIndent=0.0;

    paraStyle.paragraphSpacingBefore=0.0;

    paraStyle.headIndent=0;

    paraStyle.tailIndent=0;

    //设置字间距NSKernAttributeName:@1.5f

    NSDictionary*dic =@{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

    };

    NSAttributedString*attributeStr = [[NSAttributedStringalloc]initWithString:strattributes:dic];

    label.attributedText= attributeStr;

    }

    //计算UILabel的高度(带有行间距的情况)

    - (CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {

    NSMutableParagraphStyle*paraStyle = [[NSMutableParagraphStyle alloc] init];

    paraStyle.lineBreakMode=NSLineBreakByCharWrapping;

    paraStyle.alignment=NSTextAlignmentLeft;

    paraStyle.lineSpacing=RowSpacing;

    paraStyle.hyphenationFactor=1.0;

    paraStyle.firstLineHeadIndent=0.0;

    paraStyle.paragraphSpacingBefore=0.0;

    paraStyle.headIndent=0;

    paraStyle.tailIndent=0;

    NSDictionary*dic =@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@1.5f

    };

    CGSize size = [str boundingRectWithSize:CGSizeMake(width,SCREEN_HEIGHT)options:NSStringDrawingUsesLineFragmentOriginattributes:diccontext:nil].size;

    returnsize.height;

    }

    相关文章

      网友评论

          本文标题:带有行间距文字的label 取高度

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