前幾天項目需求中有個申請售后相關(guān)的送滞,售后說明是一個textview在用戶輸入emoji表情的時候就會提交申請售后失斁蟆瘤运;
原因很簡單窍霞,后端沒有存儲響應(yīng)的emoji字符編碼;
解決辦法:
1尽超、讓后端同學(xué)加上emoji的字符編碼到數(shù)據(jù)庫官撼,
2、從最開始用戶輸入的時候就禁止掉似谁;
上干貨吧:
- (void)textViewDidChange:(UITextView *)textView{
if (textView.text.length > 0) {
// 禁止系統(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;
}