1

I am trying to convert the emoji encoding string to character from JSON string.Some emoji are converting and some are not.

Function I am using:

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" is the emoji string of character.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

See this answer or for displaying emotions you can use NSAttributedString

NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[text dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

For example, create an extension for 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
Alexey Kudlay
  • 525
  • 2
  • 14
  • 2
    @Vinodh, Swift version that was edited in doesn't look like Objective C version **at all**. If you wanted to show your idea you should've post it in your own answer. – user28434'mstep Jan 16 '18 at 11:36
  • It's not my idea. it was mentioned in the answer what @Alexey Kudlay has mentioned. Please read then start to scold me – Vinodh Jan 16 '18 at 12:20
  • @Vinodh, Yes, he mentioned other answer as a link, but your edit marks **two completely different** code snippets as **ObjC** and **Swift** versions of answer. They are not versions, they are two different solutions! And can be both represented in Objective C and Swift. – user28434'mstep Jan 16 '18 at 14:38
  • Please revert back the original answer – Vinodh Jan 17 '18 at 06:19
  • @Vinodh, well, i've tried, but it wasn't peer approwed. – user28434'mstep Jan 17 '18 at 08:31
  • it's not my fault – Vinodh Jan 17 '18 at 08:35