Emojis弄乱了obj-c的sizeWithFont数学

问题描述 投票:17回答:3

在需要显示一长串聊天对话(通常包含表情符号)的UITableView中,会发生大小计算错误。

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

我认为这是因为字符串“:-)”比实际的笑脸图标更宽。

证据可以在这里看到:

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

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

巴姆,同样的结果。

有谁知道怎么反击这个?是否有boolean检查“标签是否已完成表情符号处理”,所以我可以跳过该电话?

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

显示的列在自定义单元格的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath段中运行。我也可以在一个完美的常规UITableViewCell上复制错误,所以这似乎不是它。

objective-c uitableview nsstring uilabel emoji
3个回答
13
投票
- (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;

}

6
投票

谢谢@SergiSolanellas!这是一个带有referencedString的版本,缩短了方法,因为文本和字体已经设置好了。

//
// 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;
}

0
投票

我用的是UILabel

sizeThatFits(_ size:CGSize) - > CGSize

它对我有用。

我的代码

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

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

© www.soinside.com 2019 - 2024. All rights reserved.