Update:The network spread emoji Code pointNot complete,I follow Wikipedia-refresh a bit,The paper version of the code was updated Swift。
Many times we need to determine a character、Or that there is not a word that contains the emoji,Use Swift language development app is no exception,For example, you can use a regular expression - but unfortunately,It seems different language support for regular expressions distinction, \in This feature is not so that's where all,So the most simple and crude way,We get a string,It contains all that may be used emoji,Then to retrieve it:
1 2 3 4 |
var emojis = "???" if let range = emojis.range(of:"xxx") { } |
In fact, it,Be conscience Kazakhstan,Of course, the efficiency is also good ......,The result is that you have to constantly update the list - and you have to put the card was a force Xcode state。
Use traversal
so,There is a medium with OC achieve extended,It seems very wide spread:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
@implementation NSString (Emoji) + (BOOL)stringContainsEmoji:(NSString *)string { __block BOOL returnValue = NO; [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { const unichar hs = [substring characterAtIndex:0]; if (0xd800 <= hs && hs <= 0xdbff) { if (substring.length > 1) { const unichar ls = [substring characterAtIndex:1]; const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000; if (0x1d000 <= uc && uc <= 0x1f77f) { returnValue = YES; } } } else if (substring.length > 1) { const unichar ls = [substring characterAtIndex:1]; if (ls == 0x20e3) { returnValue = YES; } } else { if (0x2100 <= hs && hs <= 0x27ff) { returnValue = YES; } else if (0x2B05 <= hs && hs <= 0x2b07) { returnValue = YES; } else if (0x2934 <= hs && hs <= 0x2935) { returnValue = YES; } else if (0x3297 <= hs && hs <= 0x3299) { returnValue = YES; } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) { returnValue = YES; } } }]; return returnValue; } |
In fact, its principle is to match the range where the Emoji,so,If we apply the Swift statements and their characteristics,So is this version Swift:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
extension String { var containsEmoji: Bool { for scalar in unicodeScalars { switch scalar.value { case 0x00A0...0x00AF, 0x2030...0x204F, 0x2120...0x213F, 0x2190...0x21AF, 0x2310...0x329F, 0x1F000...0x1F9CF: return true default: continue } } return false } } |
?Is not it simple?
Code from:Check if Swift string contains an emoji or dingbat charater
Original article written by LogStudio:R0uter's Blog » Swift is determined whether there is a string expression Emoji
Reproduced Please keep the source and description link:https://www.logcg.com/archives/2504.html
Why not use regular?
The same is true with the regular principle,Match emoji interval range,Can use regular。When I was writing this article, I did n’t think I could use regular,I'm not skilled at this。
But because emoji has been evolving,The final result is that I chose to store emoji separately and record the data source,Fundamentally distinguished them,No need to judge。(Of course this is for my own business logic)
Since emoji is longer,I think it's easier to judge ASCII characters in reverse,If it contains Chinese, add the Chinese character interval.。
Old iron you made this a bug oh。In the real machine nine key keyboard pinyin。 You are not allowed detection、
Yes,This does not cover all range of emoji,Because the emoji range itself is an evolution of the enlarged,You want to change the range of values of the change,For example, I added the following code,This is not a few in the body that range。
if (self.range(of: "8️⃣") != nil) { return true } else if (self.range(of: "0️⃣") != nil) { return true } else if (self.range(of: "6️⃣") != nil) { return true }