Objective c 表情符号与obj-c';s sizeWithFont数学

Objective c 表情符号与obj-c';s sizeWithFont数学,objective-c,uitableview,nsstring,uilabel,emoji,Objective C,Uitableview,Nsstring,Uilabel,Emoji,在UITableView中,如果需要显示一长串类似聊天的对话(通常包含表情),则会发生大小计算错误 我的问题是,如果字符串的长度正好合适,并且我使用了sizeWithFont,我在第一次测量时使用sizeWithFont得到的字符串长度不正确,导致“换行” 我假设这是因为字符串“:-”比实际的笑脸图标宽 证据可以在这里看到: SizeWithFont“> 现在,在其他一些堆栈中,有人声称sizeWithFont将只解释字符串,而不是表情符号,这对我来说没有意义,因为它“最终”会正确的 但他们建议

UITableView
中,如果需要显示一长串类似聊天的对话(通常包含表情),则会发生大小计算错误

我的问题是,如果字符串的长度正好合适,并且我使用了
sizeWithFont
,我在第一次测量时使用
sizeWithFont
得到的字符串长度不正确,导致“换行”

我假设这是因为字符串“:-”比实际的笑脸图标宽

证据可以在这里看到:

SizeWithFont“>

现在,在其他一些堆栈中,有人声称
sizeWithFont
将只解释字符串,而不是表情符号,这对我来说没有意义,因为它“最终”会正确的

但他们建议改用sizeToFit,所以我决定试一试

砰,同样的结果

有人知道如何应对这种情况吗?是否有一个
布尔值
来检查“标签是否已完成表情处理”,这样我就可以跳过该调用

sizeWithFont
意识到它的错误之前,运行同一行两次没有任何作用,似乎需要绘制视图


显示的列在
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)中运行indexPath
段,在自定义单元格上。我也可以在完全规则的UITableViewCell上复制错误,所以看起来不是这样。

谢谢@SergiSolanellas!这是一个采用AttributeString的版本,它缩短了方法,因为文本和字体已经设置好了

- (CGFloat)heightStringWithEmojis:(NSString*)str fontType:(UIFont *)uiFont ForWidth:(CGFloat)width {

// Get text
CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef) str );
CFIndex stringLength = CFStringGetLength((CFStringRef) attrString);

// Change font
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef) uiFont.fontName, uiFont.pointSize, NULL);
CFAttributedStringSetAttribute(attrString, CFRangeMake(0, stringLength), kCTFontAttributeName, ctFont);

// Calc the size
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CFRange fitRange;
CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(width, CGFLOAT_MAX), &fitRange);

CFRelease(ctFont);
CFRelease(framesetter);
CFRelease(attrString);

return frameSize.height + 10;

}
//
// Given an attributed string that may have emoji characters and the width of 
// the display area, return the required display height.
//
- (CGFloat)heightForAttributedStringWithEmojis:(NSAttributedString *)attributedString forWidth:(CGFloat)width {
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);
    CFRange fitRange;
    CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(width, CGFLOAT_MAX), &fitRange);

    CFRelease(framesetter);

    return frameSize.height;
}
我正在使用UILabel

尺寸大小适合(usize:CGSize)->CGSize

这对我有用

我的代码

let tempLabel = UILabel()
tempLabel.font = font
tempLabel.attributedText = attText
tempLabel.numberOfLines = 0
let size = tempLabel.sizeThatFits(CGSize(width: 200, height:CGFloat.greatestFiniteMagnitude))

作为代码,您需要分配三个属性。

您是在调整标签大小还是单元格大小?如果只有标签在调整大小,我想我会使用自动布局来完成所有的魔术。以编程方式计算标签大小容易出错。我发现这会返回良好的测量结果,但我没有尝试使用表情符号-[UILabel textRectForBounds:LimitedToNumberOfflines:]从iOS 7开始,
sizeWithFont:constrainedToSize:lineBreakMode
已被弃用,取而代之的是
boundingRectWithSize:options:attributes:context:
你刚刚成就了我的一天,我为此奋斗了数小时,你的解决方案就像一个魔咒一样……放手!你好@SergiSolanellas,你太棒了:)。我已经花了一天时间来获取此库的解决方案[STTweetLabel]和您的答案对我非常有帮助..非常感谢。您好@SergiSolanellas。我需要您的帮助。在我的文本中有这么多新行('\n'))字符是包含的,并且当时文本中没有表情符号。这个函数给了我错误的高度。有什么解决方案吗???有人有swift版本吗?奇怪的是,在iOS 10中,我观察到当属性字符串包含表情符号时,这个函数产生了错误的高度。