0

I have following string, and i want to show it in a UILabel properly with emojies and the new lines. Also I want to draw it using drawInRect method. How do I get them converted/Encoded/Decoded properly?

This string will change on runtime so should show any unicode character/ emoji or special characters such as \n or &

I'm sorry that I do not know proper terms to use to ask this question. Which makes it difficult for me to find an answer online. My knowledge about this topic is very low.

\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude05\ud83d\ude06 \u0db8\u0da0\u0d82 \u0d91\u0d9a\u0dca\u0d9a\n#set_with_machan\nkunuharapa na \n#Follow @lankan_machan\nhttps://www.instagram.com/lankan_machan

after encoding the text should look like this with emojis, unicode characters & new lines. enter image description here

NSGodMode
  • 599
  • 1
  • 6
  • 16
  • 1
    I'm guessing you're trying to decode escaped unicode characters. I'm not entirely sure if this will help, but quickly searching for how to unescape unicode characters in objective-c I found [this](http://stackoverflow.com/questions/7860867/converting-escaped-utf8-characters-back-to-their-original-form) – DanZimm Nov 14 '16 at 21:15
  • @DanZimm Thank you very much. I searched with the terms and found a solution, Edited a bit. I will put that as an answer – NSGodMode Nov 14 '16 at 21:58

1 Answers1

1

I was able to find a solution and Edited it a bit. This un escapes the unicode characters perfectly and shows them properly. Shows the new lines too. Thanks @DanZimm for help.

- (NSString*) unescapeUnicodeString2:(NSString*)string
{
    NSString* esc1 = [string stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    NSString* esc2 = [esc1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString* quoted = [[@"\"" stringByAppendingString:esc2] stringByAppendingString:@"\""];
    NSData* data = [quoted dataUsingEncoding:NSUTF8StringEncoding];

    NSString* unesc = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:NULL];
    assert([unesc isKindOfClass:[NSString class]]);
    return unesc;
}

Solution found here I had to edit it because one of the methods were deprecated.

Community
  • 1
  • 1
NSGodMode
  • 599
  • 1
  • 6
  • 16