一般方法
+ (CGFloat)heightForLableWithText:(NSString *)text Font:(UIFont*)font AndlableWidth:(CGFloat)lableWidth
{
CGSize textSize = CGSizeMake(lableWidth, CGFLOAT_MAX);
//計(jì)算高度
CGSize sizeWithFont = [text boundingRectWithSize:textSize options: NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading|NSStringDrawingUsesDeviceMetrics attributes:@{NSFontAttributeName:font} context:nil].size;
return ceil(sizeWithFont.height);
}
帶表情符號(hào)的時(shí)候string高度比不帶的高2pt..怎么計(jì)算?
方式一
網(wǎng)上找來(lái)的一種取巧的計(jì)算方法
// 轉(zhuǎn)換表情
if ([tempDic[@"content"] respondsToSelector:@selector(length)]) {
[self setValue:[ConvertToCommonEmoticonsHelper convertToSystemEmoticons:tempDic[@"content"]] forKey:@"content"];
self.attributedStringArray = [self expressionAttributedStringWithAttributedString:[[NSAttributedString alloc] initWithString:self.content]];
if (self.attributedStringArray.count) {
NSMutableString *string = [NSMutableString string];
NSMutableString *originalString = [NSMutableString string];
// 如果有
// NSLog(@"數(shù)組:%@", self.attributedStringArray);
int beginLength = 0;
int faceCount = 0;
//添加一張圖片
for (int i = 0; i < self.attributedStringArray.count; i ++) {
id dic = self.attributedStringArray;
if ([dic isKindOfClass:[NSString class]]) {
// 是string
[string appendString:dic];
[originalString appendString:dic];
} else if ([dic isKindOfClass:[NSDictionary class]]){
// 是表情圖片
[dic setValue:@([dic[@"location"] intValue] - beginLength) forKey:@"location"];
beginLength += [dic[@"length"] intValue];
faceCount++;
// 僅用于還原高度
[originalString appendString:@"換"];
}
}
// 修正計(jì)算誤差
for (int i = 0; i < (int)(faceCount/2.5); i ++) {
[originalString appendString:@"?"];
}
self.content = string;
self.originalContent = originalString;
}
}
方式二 采用c底層方法
+ (CGFloat)heightForLableWithText:(NSAttributedString *)text lableWidth:(CGFloat)lableWidth {
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)text);
CGSize targetSize = CGSizeMake(lableWidth, CGFLOAT_MAX);
CGSize size = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, (CFIndex)[text length]), NULL, targetSize, NULL);
CFRelease(framesetter);
//取整
#if defined(__LP64__) && __LP64__
return ceil(size.height);
#else
//向上
return ceilf(size.height) ;
#endif
}
帶linebreak屬性->ios7
+ (CGFloat)heightForLableWithString:(NSString *)text lableWidth:(CGFloat)lableWidth font:(UIFont *)font
{
CGSize textSize = CGSizeMake(lableWidth , CGFLOAT_MAX);
if ([UIDevice currentDevice].systemVersion.floatValue >=7) {
NSMutableParagraphStyle *parStyle=[[NSMutableParagraphStyle alloc]init];
parStyle.lineBreakMode=NSLineBreakByWordWrapping;
NSDictionary *attributes=@{NSFontAttributeName:font,NSParagraphStyleAttributeName:parStyle };
return ceil([text boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height)
;
}else{
return ceil([text sizeWithFont:font forWidth:lableWidth lineBreakMode:UILineBreakModeWordWrap].height);
}
}