gpt4 book ai didi

ios - boundingRectWithSize 与表情符号崩溃

转载 作者:行者123 更新时间:2023-12-01 17:41:14 26 4
gpt4 key购买 nike

当我使用boundingRectWithSize计算文本的高度时,它崩溃了。我发现可能是因为文本中有一些表情符号。有人可以告诉我如何解决这个问题。

    CGFloat kScreenMargin = 15.;
CGFloat kScreenWidth = [UIScreen mainScreen].bounds.size.width;

self.content = @"Eefggg👨🏻‍🍳👩🏻‍🍳";

CGFloat contentH = [self.content boundingRectWithSize:CGSizeMake(kScreenWidth - 2 * kScreenMargin, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height;

Here is the crash screenshot

最佳答案

首先转换为编码字符串,然后获取高度:

CGFloat kScreenMargin = 15.;
CGFloat kScreenWidth = [UIScreen mainScreen].bounds.size.width;
NSString *strEmo = @"Eefggg👨🏻‍🍳👩🏻‍🍳"
NSData *data = [strEmo dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
self.content = goodValue;
CGFloat contentH = [self.content boundingRectWithSize:CGSizeMake(kScreenWidth - 2 * kScreenMargin, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height;

关于ios - boundingRectWithSize 与表情符号崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42500474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com