String 各种计算高度

一般方法

+ (CGFloat)heightForLableWithText:(NSString *)text Font:(UIFont*)font AndlableWidth:(CGFloat)lableWidth

{

CGSize textSize = CGSizeMake(lableWidth, CGFLOAT_MAX);

//计算高度

CGSize sizeWithFont = [text boundingRectWithSize:textSize options: NSStringDrawingUsesLineFragmentOrigin |

NSStringDrawingUsesFontLeading|NSStringDrawingUsesDeviceMetrics attributes:@{NSFontAttributeName:font} context:nil].size;

return ceil(sizeWithFont.height);

}

带表情符号的时候string高度比不带的高2pt..怎么计算?

方式一

网上找来的一种取巧的计算方法

// 转换表情

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(@"数组:%@", 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:@"换"];

}

}

// 修正计算误差

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);

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容