问题
I have a UITextView with text:
🅰🅱🍉👌📧🎏❡
its image:
(This is screen shot of my text, I worry somebody can not see my special character)
If I am an end-user, I just see that there are only 7 characters on textView.
But [textView.text length]=13
I want to split this text into an array like this:
array = @[
,
,
,... ]
But I can't detect where
is, where
is, where
is ... :(
Could you help me!
回答1:
Characters outside of the "basic multilingual plane" - in other words, characters whose
Unicode value is greater than U+FFFF - are stored in NSString as two UTF-16 characters,
a so-called surrogate pair.
For example, "👌" = U+1F44C (OK HAND SIGN)
is stored in NSString as the two characters U+D83D, U+DC4C.
To correctly split a string with surrogate pairs, use the
enumerateSubstringsInRange:options:usingBlock:
method of NSString with the NSStringEnumerationByComposedCharacterSequences option.
Another useful method is rangeOfComposedCharacterSequenceAtIndex: to determine the
range of a composed character sequence at a certain position.
来源:https://stackoverflow.com/questions/22499381/recognize-special-characters-in-nsstring