用字符

时间:2015-10-12 11:29:27

标签: ios objective-c replace emoji

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

  

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

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表情符号。

由于

2 个答案:

答案 0 :(得分:2)

试试这个

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);

答案 1 :(得分:0)

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

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)"];