iOS textView

2017-11-28  本文已影响13人  夏天爱西瓜汁

2016.11.8

- (void)textViewDidChange:(UITextView*)textView{

if(textView.text.length>0) {

//禁止系统表情的输入

NSString*text = [selfdisable_emoji:[textViewtext]];

if(![textisEqualToString:textView.text]) {

NSRangetextRange = [textViewselectedRange];

textView.text= text;

[textViewsetSelectedRange:textRange];

}

//解决限制字数的条件下,中文联想还可以继续输入的问题

NSIntegerlength= textView.text.length;

if(length >100) {

NSString*memo = [textView.textsubstringWithRange:NSMakeRange(0,100)];

self.feedbackContentTextView.text= memo;

}

}

}

//限制Emoji表情的输入

- (NSString*)disable_emoji:(NSString*)text{

NSRegularExpression*regex = [NSRegularExpressionregularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitiveerror:nil];

NSString*modifiedString = [regexstringByReplacingMatchesInString:textoptions:0range:NSMakeRange(0, [textlength])withTemplate:@""];

returnmodifiedString;

}

上一篇下一篇

猜你喜欢

热点阅读