ios 在UILabel中显示表情符号?

s5a0g9ez  于 2023-05-30  发布在  iOS
关注(0)|答案(1)|浏览(487)

我已经配置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仍然显示表情符号&amp文本)?注意,我使用的是Drupal,并且启用了'Unicode'模块,所以我想这就是为什么编码格式是&#x1F388的原因。

- (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 = @{
        @"&" : @"&",
        @""" : @"\"",
        @"'" : @"'",
        @"&lt;" : @"<",
        @"&gt;" : @">",
        // 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) {
                
                
             
            }];
lndjwyie

lndjwyie1#

找到了一个超级简单的解决我的问题的方法。把它贴在这里,以防别人看到。返回的原始数据字符串:

Birthday emoji &amp;#x1F388;

下面是我最终使用的代码,使表情符号出现在我的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;
               
               
              });

相关问题