我已经配置MySQL表来存储表情符号,如下:
ALTER TABLE field_data_field_notes MODIFY field_notes_value
VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
也就是说,当我将字符串返回到我的应用程序并尝试在UILabel中显示它们时,它们显示如下:
Birthday emoji 🎈
Emoji是一个气球。我试过下面的代码来解码它并显示实际的表情符号,但它似乎不起作用(例如:UILLabel仍然显示表情符号&文本)?注意,我使用的是Drupal,并且启用了'Unicode'模块,所以我想这就是为什么编码格式是🎈的原因。
- (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;
}
下面是我用来将字符串保存到数据库的代码:
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) {
}];
1条答案
按热度按时间lndjwyie1#
找到了一个超级简单的解决我的问题的方法。把它贴在这里,以防别人看到。返回的原始数据字符串:
下面是我最终使用的代码,使表情符号出现在我的UILabel中。
ViewController.m