在開(kāi)發(fā)中,可能會(huì)遇到服務(wù)器后臺(tái)數(shù)據(jù)庫(kù)不能識(shí)別IOS系統(tǒng)表情,導(dǎo)致存儲(chǔ)出錯(cuò)的問(wèn)題,所以就需要禁止系統(tǒng)emoji表情的輸入,代碼如下:
- (void)textViewDidChange:(UITextView *)textView{
// 如果當(dāng)前有帶選項(xiàng),說(shuō)明輸入還未完成,所以需要忽略掉,出現(xiàn)這種情況一種操作是使用繁體-注音鍵盤,點(diǎn)擊注音,會(huì)顯示待選項(xiàng)
if (textView.text.length > 0 && !textView.markedTextRange) {
// 禁止系統(tǒng)表情的輸入
NSString *text = [self disable_emoji:textView.text];
if (![text isEqualToString:textView.text]) {
NSRange textRange = [textView selectedRange];
textView.text = text;
[textView setSelectedRange:textRange];
}
}
}
- (NSString *)disable_emoji:(NSString *)text{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitive error:nil];
NSString *modifiedString = [regex stringByReplacingMatchesInString:text
options:0
range:NSMakeRange(0, text.length)
withTemplate:@""];
return modifiedString;
}