我正在尝试将表情符号编码字符串转换为JSON字符串中的字符。一些表情符号正在转换,而另一些则没有。
我正在使用的功能:
extension String {
var decodeEmoji: String? {
let data = self.data(using: String.Encoding.utf8)
let decodedStr = NSString(data: data!, encoding: String.Encoding.nonLossyASCII.rawValue)
if decodedStr != nil {
return decodedStr as String?
}
return self
}
}
JSON string: Altre novità in arrivo oggi... \ud83d\udcaa\ud83c\udffb
“\ud83d\udcaa\ud83c\udffb
”是表情符号的表情符号。
答案 0 :(得分:0)
查看此answer或显示您可以使用的情感NSAttributedString
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[text dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
例如,为UILabel
@implementation UILabel (Emotion)
- (void)setHtmlText:(NSString *)text
{
self.text = text;
NSDictionary *attributes = [self.attributedText attributesAtIndex:0 longestEffectiveRange:nil inRange:NSMakeRange(0, self.attributedText.length)];
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[text dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
[attrStr addAttributes:attributes range:NSMakeRange(0, attrStr.length)];
self.attributedText = attrStr;
}
@end