Emoji NSTextStorage

I am currently working on a custom NSTextStorage in iOS, I need to detect some hashtags (#), mentions and urls ... Everything is working fine at the moment, but if the user tries to enter an emoji char from the keyboard it doesn't show up because I I'm using the Helvetica font.

I am using regular expresiรณn to detect hashtags, mentions and urls this way:

NSRegularExpression *iExpression = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                             options:0
                                                                               error:NULL];

NSRange paragraphRange = [self.string paragraphRangeForRange: self.editedRange];        

[iExpression enumerateMatchesInString:self.string
                              options:0
                                range:paragraphRange
                           usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
    // Changing format of match
}

      

Where pattern

is a valid regex pattern to find hashtags, urls, or mentions.

Is there any pattern for detecting emoji symbols? what pattern should i use, i have tried this (and many others) [^\\x{1F601}-\\x{1F64F}\r\n]

with no success.

Thank.

+3


source to share


1 answer


I'm not sure about the iOS regex patterns, but I'm pretty sure that while you are pointing to the correct range, you are excluding the character set rather than including using a carriage character ^

within the character set definition []

. Also I don't think you need to avoid those hex points and match CR and LF.

There is a complete Unicode table from Emoji composed of the corresponding Unicode dots here , which I have compiled in one expression below:



([0-9#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?

      

In PCRE color, adding a checkbox UTF-16

is a must, typing u

in the modifiers part to match Unicode characters, so it will be /ABOVE-EXPRESSION/u

, but unfortunately I don't know how this is done in your particular case.

0


source







All Articles