I've configured MySQL tables to store emojis with the following:
ALTER TABLE field_data_field_notes MODIFY field_notes_value
VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
That said, when I return strings to my app and try to display them in a UILabel, they appear like this:
Birthday emoji 🎈
The emoji is a balloon. I've tried the below code to decode it and display the actual emoji, but it doesn't seem to work (e.g. the UILabel still displays the emoji & text)? Note, I'm using Drupal and I have the 'Unicode' module enabled, so I imagine this is why the encoded format is 🎈.
- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
TimeTableViewCell *cell = (TimeTableViewCell *)[self.subtableView dequeueReusableCellWithIdentifier:WeeklyTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimeTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString *encodedString =[self.sortedByTime valueForKey:@"notes"][indexPath.row];
NSString *decodedString = [self decodeString:encodedString];
cell.notes.text = decodedString;
}
- (NSString *)decodeString:(NSString *)string {
// Decode HTML entities
NSDictionary *entities = @{
@"&" : @"&",
@""" : @"\"",
@"'" : @"'",
@"<" : @"<",
@">" : @">",
// Add more entity mappings as needed
};
NSMutableString *decodedString = [NSMutableString stringWithString:string];
for (NSString *entity in entities) {
NSString *replacement = [entities objectForKey:entity];
[decodedString replaceOccurrencesOfString:entity withString:replacement options:NSLiteralSearch range:NSMakeRange(0, [decodedString length])];
}
// Decode Unicode escape sequences
NSString *decodedUnicodeString = [decodedString stringByReplacingOccurrencesOfString:@"&#x" withString:@"\\U0000"];
NSData *data = [decodedUnicodeString dataUsingEncoding:NSUTF8StringEncoding];
NSString *decodedEmojiString = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];
return decodedEmojiString ?: decodedString;
}
Here is the code I'm using to save the string to my database:
NSDictionary *notesField = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:self.notesField.text, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
NSDictionary *notesFieldcontent = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:notesField] forKey:@"und"];
[self.nodeData setObject:notesFieldcontent forKey:@"field_notes"];
[DIOSNode nodeSave:self.nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
Found a super simple solution to my question. Posting it here incase anyone else runs into it. My original data string that was returned:
Birthday emoji &#x1F388;
Here's the code I ended up using to make the emoji appear in my UILabel.
ViewController.m
NSString *htmlEntity = [self.finalTimes valueForKey:@"notes"][indexPath.row];
NSString *replacementString = [htmlEntity stringByReplacingOccurrencesOfString:@"amp;" withString:@""];
dispatch_async(dispatch_get_main_queue(), ^{
NSData *data = [replacementString dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
UIFont *font = [UIFont fontWithName:@"Avenir-Oblique" size:11.0];
UIColor *textColor = [UIColor darkGrayColor]; // Set the correct font name and size
NSDictionary *attributes = @{NSFontAttributeName: font, NSForegroundColorAttributeName: textColor};
NSMutableAttributedString *styledAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
[styledAttributedString addAttributes:attributes range:NSMakeRange(0, styledAttributedString.length)];
cell.notes.attributedText = styledAttributedString;
});