在開發中,可能會遇到服務器后臺數據庫不能識別IOS系統表情,導致存儲出錯的問題,所以就需要禁止系統emoji表情的輸入,代碼如下:
- (void)textViewDidChange:(UITextView *)textView{
// 如果當前有帶選項,說明輸入還未完成,所以需要忽略掉,出現這種情況一種操作是使用繁體-注音鍵盤,點擊注音,會顯示待選項
if (textView.text.length > 0 && !textView.markedTextRange) {
// 禁止系統表情的輸入
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;
}