UILabel,DTAttributedLabel 关于计算高度
2013-06-23 20:52
579 查看
UILabel 在UITableView中显示时一般要提前计算好高度,网上普遍的解决方案是用constrainedToSize函数
但是有个缺陷,如果长度太长,高度就可能算不准了。
对于DTAttributedLabel就更加算不准确了。
我的解决方案是用UIView自带的sizeToFit函数,先将view的高度设得最大,设置文字,然后sizeToFit就会得到最精确的高度,这种效率肯定没有上面的高。不过能很好的解决问题,下面给出DTAttributedLabel高度的函数。
CGSize strSize = [str sizeWithFont:font constrainedToSize:CGSizeMake(_w, 9999) lineBreakMode:UILineBreakModeWordWrap];
但是有个缺陷,如果长度太长,高度就可能算不准了。
对于DTAttributedLabel就更加算不准确了。
我的解决方案是用UIView自带的sizeToFit函数,先将view的高度设得最大,设置文字,然后sizeToFit就会得到最精确的高度,这种效率肯定没有上面的高。不过能很好的解决问题,下面给出DTAttributedLabel高度的函数。
+(float)getStringHeight:(NSString*)str Font:(UIFont*)font Wide:(float)_w{ DTAttributedLabel* dtLabel = [[DTAttributedLabel alloc] initWithFrame:CGRectMake(0,0, _w, 9999)]; dtLabel.lineBreakMode = UILineBreakModeWordWrap; CGFloat gapHeight = DT_LABEL_GAPH; CTParagraphStyleSetting gap; gap.spec = kCTParagraphStyleSpecifierLineHeightMultiple; gap.value = &gapHeight; gap.valueSize = sizeof(float); CGFloat maxHeight = DT_LINE_MAX_HEIGHT; CTParagraphStyleSetting maxLineHeight; maxLineHeight.spec = kCTParagraphStyleSpecifierMaximumLineHeight; maxLineHeight.value = &maxHeight; maxLineHeight.valueSize = sizeof(float); CTParagraphStyleSetting settings[] = { gap, maxLineHeight }; CTParagraphStyleRef style = CTParagraphStyleCreate(settings,sizeof(settings)/sizeof(CTParagraphStyleSetting)); NSMutableAttributedString* attributeStr = [[NSMutableAttributedString alloc] initWithString:str]; [attributeStr addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(id)style range:NSMakeRange(0, [attributeStr length])]; CFRelease(style); CTFontRef tempFont = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, NULL); [attributeStr addAttribute:(NSString*)kCTFontAttributeName value:(id)tempFont range:NSMakeRange(0, [attributeStr length])]; CFRelease(tempFont); CTFontRef emojiFont = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize - EMOJI_SIZE_OFFSET, NULL); NSArray* emojiRanges = [attributeStr.string emojiRanges]; for (int i = 0; i < [emojiRanges count]; i++) { NSString* rangeStr = [emojiRanges OBJECT_AT(i)]; NSRange emojiRange = NSRangeFromString(rangeStr); [attributeStr addAttribute:(NSString*)kCTFontAttributeName value:(id)emojiFont range:emojiRange]; } CFRelease(emojiFont); [dtLabel setAttributedString:attributeStr]; [dtLabel sizeToFit]; float h = frameH(dtLabel); [dtLabel release]; [attributeStr release]; return h; }
相关文章推荐
- TTTAttributedLabel 高度计算
- [iOS学习]关于UILabel计算多行高度以及显示的问题
- [置顶] UILabel根据文本、字体大小计算label宽度;以及自适应高度
- iOS开 4000 发时关于UILabel文本高度和宽度的计算问题(单行、多行)
- 关于用boundingRectWithSize计算label自适应高度那点事
- 八,iOS当UILabel文字不固定时计算label的高度
- 根据字体数量和字体的型号 动态计算Label的高度
- 关于ScrollView中嵌套listView,textView存在多行时,listView高度计算不正确。
- 计算文本高度 UILabel和sizeWithFont方法
- label自动换行并且计算其高度
- iOS 计算label的宽度和高度
- iOS UILabel设置行间距和字间距并计算高度
- iOS 计算label的高度
- iOS7中计算UILabel中字符串的高度
- boundingRectWithSize 的使用,计算Label高度
- 根据宽度计算Label的高度
- ios 计算label宽度(高度)出错在iPhone5s、iphone6上出错
- 根据Label的文字内容动态计算Label高度
- iOS Dev (4) 用TTTAttributedLabel创建变化丰富的UILabel
- iOS UILabel设置行间距和字间距并计算高度