iOS表情符号在UILabel中混乱 [英] iOS emoji messed up in UILabel

查看:248
本文介绍了iOS表情符号在UILabel中混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





只是想知道为什么会发生这种情况,什么可能是一个可能的修复我应该看看..



谢谢!

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

  NSData * data = [content dataUsingEncoding:NSUTF8StringEncoding]; 
NSString * decoded = [[NSString alloc] initWithData:数据编码:NSNonLossyASCIIStringEncoding];


解决方案

我有一个解决方案,用表情符号查找高度。
请使用< CoreText / CoreText.h> 框架,并使用以下代码。

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

//获取文本
CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault,0);
CFAttributedStringReplaceString(attrString,CFRangeMake(0,0),(CFStringRef)str);
CFIndex stringLength = CFStringGetLength((CFStringRef)attrString);

//更改字体
CTFontRef ctFont = CTFontCreateWithName((__ bridge CFStringRef)uiFont.fontName,uiFont.pointSize,NULL);
CFAttributedStringSetAttribute(attrString,CFRangeMake(0,stringLength),kCTFontAttributeName,ctFont);

//计算大小
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);

返回frameSize.height +4;

}

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


I am using a open source UILabel subclass STTweetLabel v2.22 (Github) and trying to show emoji in the label. During my test it seems that the code can handle most cases correctly but sometimes I see this:

Just wondering why this could happen, and what could be a possible fix I should look into..

Thanks!

-- Update (adding code used to decode strings from server) --

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

解决方案

Hi I have got one solution for that library you can find height with emoji. Please use <CoreText/CoreText.h> framework and use below code.

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

}

Let me know thoughts....!!!

这篇关于iOS表情符号在UILabel中混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆