Last active
August 29, 2015 14:17
-
-
Save danielbuechele/36cf91edf3ea0da6a4a6 to your computer and use it in GitHub Desktop.
HTML encode emojis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString *text = @"😀abc🇯🇵"; | |
NSMutableString *convertedText = @"".mutableCopy; | |
[text enumerateSubstringsInRange:NSMakeRange(0,[text length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { | |
unsigned int i = (unsigned int)*(const UInt32 *)[[substring dataUsingEncoding:NSUTF8StringEncoding] bytes]; | |
if (i > 8436960) { | |
NSData *data = [substring dataUsingEncoding:NSUTF32BigEndianStringEncoding]; | |
NSString *str = [NSString stringWithFormat:@"%@",data]; | |
str = [str substringWithRange:NSMakeRange(1, [str length]-2)]; | |
for (NSString *c in [str componentsSeparatedByString:@" "]) { | |
[convertedText appendString:[NSString stringWithFormat:@"&#x%@;",c]]; | |
} | |
} else { | |
[convertedText appendString:substring]; | |
} | |
}]; | |
NSLog(@"%@",convertedText); | |
NSData *stringData = [convertedText dataUsingEncoding:NSUTF8StringEncoding]; | |
NSAttributedString *attrString =[[NSAttributedString alloc]initWithData:stringData options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil]; | |
NSString *finalString = attrString.string; | |
NSLog(@"%@",finalString); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment