首页 > 其他 > 详细

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

时间:2016-09-04 00:08:40      阅读:188      评论:0      收藏:0      [点我收藏+]

#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 = [[NSMutableParagraphStylealloc] init];

    paraStyle.lineBreakMode =NSLineBreakByCharWrapping;

    paraStyle.alignment =NSTextAlignmentLeft;

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

    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 = [[NSMutableParagraphStylealloc] init];

    paraStyle.lineBreakMode =NSLineBreakByCharWrapping;

    paraStyle.alignment =NSTextAlignmentLeft;

    paraStyle.lineSpacing = UILABEL_LINE_SPACE;

    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 = [strboundingRectWithSize:CGSizeMake(width,HEIGHT) options:NSStringDrawingUsesLineFragmentOriginattributes:dic context:nil].size;

    return size.height;

}

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

原文:http://www.cnblogs.com/isItOk/p/5838310.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!