标签:
1.根据文字计算Label的宽度
1 +(CGFloat)widthOfString:(NSString *)string font:(UIFont *)font height:(CGFloat)height 2 { 3 NSDictionary * dict=[NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName]; 4 CGRect rect=[string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil]; 5 return rect.size.width; 6 }
2.根据文字计算Label的高度
2.1方法一:sizeThatFits
例如:CGSize size = [toolLabel sizeThatFits:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT)];
得到的size.height就是Label的高度
2.2方法二:
+(CGFloat)heightOfString:(NSString *)string font:(UIFont *)font width:(CGFloat)width { CGRect bounds; NSDictionary * parameterDict=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; bounds=[string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:parameterDict context:nil]; return bounds.size.height; }
标签:
原文地址:http://www.cnblogs.com/liaods/p/4801909.html