【问题标题】:iOS emoji messed up in UILabeliOS 表情符号在 UILabel 中搞砸了
【发布时间】:2014-09-27 12:21:27
【问题描述】:

我正在使用开源 UILabel 子类 STTweetLabel v2.22 (Github) 并尝试在标签中显示表情符号。在我的测试过程中,代码似乎可以正确处理大多数情况,但有时我会看到:

只是想知道为什么会发生这种情况,以及我应该研究什么可能的解决方法..

谢谢!

-- 更新(添加用于从服务器解码字符串的代码)--

 NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
 NSString *decoded = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];

【问题讨论】:

  • 你好@Allan Jiang,我也面临这个问题,你能帮帮我吗?你有什么解决办法吗?
  • @JayMehta 我没有任何有效的解决方案。我切换到其他图书馆
  • 能否提供其他库名?

标签: ios objective-c unicode encoding emoji


【解决方案1】:

您好,我为该库提供了一种解决方案,您可以使用表情符号找到高度。 请使用<CoreText/CoreText.h> 框架并使用以下代码。

  - (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 +4;

}

让我知道想法....!!!

【讨论】:

    【解决方案2】:

    几件事:

    不要使用encoding:NSNonLossyASCIIStringEncoding,表情符号不是ASCII。使用NSUTF8StringEncoding

    您为什么要转换为NSData,然后又转换回NSString?这没有任何意义。

    这里还有其他事情发生。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 2016-04-16
      • 2012-12-27
      相关资源
      最近更新 更多