【问题标题】:Replace string character with an image用图像替换字符串字符
【发布时间】:2015-10-12 11:29:27
【问题描述】:

我正在开发聊天应用程序中的表情符号。当有人给我发送表情符号时,我收到了这种格式的消息:- 你好...(担心)你好吗(快乐)?

(担心)和(快乐)被视为表情符号

arremojivalue=[[NSArray alloc]initWithObjects:@"(worried)",@"(sad)",@"(bandit)",@"(wink)",@"(surprised)",@"(smirking)",@"(laugh)",@"(cool)",@"(stoned)",@"(smile)",@"(nerd)",@"(happy)",@"(evil-grin)",@"(tongue)",@"(lips-sealed)",@"(GIF)",@"(dull)", nil];

当我收到消息(字符串)时,我如何检查它是否包含来自 arremojivalue 的上述单词。我想将消息字符串中的那个(担心)和(快乐)词替换为“表情符号”。

我试过这个:-

NSString *stremoji;
stremoji=[NSString stringWithFormat:@"%@",arremojivalue];

if ([message containsString:stremoji])
{
     message= [message stringByReplacingOccurrencesOfString:stremoji
                                                    withString:@"(emoji)"];

     cell.textLabel.text=message; //@"Emoji arrived";
}

首先请回答我如何将那些单词(担心)(快乐)从字符串中删除为“表情符号”。

之后我想将那些“表情符号”替换为 UIWebview 以显示 GIF 表情符号。

谢谢

【问题讨论】:

  • 我会说NSAttributedStringNSTextAttachment。但我不确定 .gif 是否易于渲染。
  • 在您的情况下使用表情符号字体是否合适?或者简单地为您的表情符号使用 unicode 值。
  • @myte 我没有为表情符号使用 unicode 值。
  • @bhavin ramani 我不是在问你是不是。我的意思是,根据您的具体情况,也许可以选择这样做。

标签: ios objective-c replace emoji


【解决方案1】:

试试这个

NSArray *arremojivalue=[[NSArray alloc]initWithObjects:@"(worried)",@"(sad)",@"(bandit)",@"(wink)",@"(surprised)",@"(smirking)",@"(laugh)",@"(cool)",@"(stoned)",@"(smile)",@"(nerd)",@"(happy)",@"(evil-grin)",@"(tongue)",@"(lips-sealed)",@"(GIF)",@"(dull)", nil];

//
NSString *message = @"Hello...(worried) how are you(happy)?";

for (NSString *emoji in arremojivalue) {
    if ([message containsString:emoji]){
       message = [message stringByReplacingOccurrencesOfString:emoji withString:YourEmojiValue];//value for (worried) = @":)"
    }
}
 NSLog(@"message updated:%@",message);

//你问题的第二部分

NSDictionary *emojiDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                 @":)",@"(worried)",
                                 @"(:D)",@"(smile)",
                                 nil];
NSString *message = @"Hello...(worried) how are you(smile)?";

for (NSString *emojiKey in emojiDictionary.allKeys) {
    if ([message containsString:emojiKey]){
        message = [message stringByReplacingOccurrencesOfString:emojiKey withString:[emojiDictionary valueForKey:emojiKey]];
    }
}
 NSLog(@"message updated:%@",message);

【讨论】:

  • 感谢@jamil65able 这解决了我的问题。但是我可以从我收到的数组中显示那个 gif 图像吗?
  • 我想在 emoji 的标签中显示 webview 我在字符串中有(担心)和(快乐)单词。
  • 是的,谢谢...这就像魅力一样,但我怎样才能使用 webview 而不是“:-)”。
【解决方案2】:

这是使用正则表达式替换的理想情况。

NSArray *emojiList = @[@"(sad)",@"(happy)",@"(angry)"] // and so on ..

NSString *regex = [emojiList componentsJoinedByString:@" | "];
regex = [string stringByReplacingOccurencesOfString:@"(" withString:@"\\("];

regex = [string stringByReplacingOccurencesOfString:@")" withString:@"\\)"];

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionCaseInsensitive error:nil];

NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"(emoji)"];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 2022-06-12
    • 2021-04-16
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    相关资源
    最近更新 更多