注意:
textFiledDidChange方法中限制输入文字数量前,不可以直接修改 textField.text = text,需要进行判断前后文字是否变化了;否则会造成高亮文本直接进入文本框内,造成问题。那么禁止输入空格和换行,推荐最好在下面的方法里面做。
//过滤相关文字
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
if (![string isEqualToString:tem]) {
return NO;
}
return YES;
}
如果你确实想在textViewDidChange中处理掉空格和换行符号,你可以这样,毕竟可以减少代码。
-(void)textViewDidChange:(UITextView *)textView
{
//最大限制字数
CGFloat maxLength = 0;
if(textView==_textView_note)
{
maxLength=500;
}
else if(textView==_textViewFloor)
{
// 去除空格和换行(判断前后文字是否变化了)
NSString *text = [textView.text trim];
if (![text isEqualToString:textView.text]) {
NSRange textRange = [textView selectedRange];
textView.text = text;
[textView setSelectedRange:textRange];
}
maxLength=50;
}
NSString *toBeString = textView.text;
//获取高亮文本范围= 临时插入的需要用户确认的文本长度.确认输入以后,变成0
//获取高亮部分
UITextRange *selectedRange = [textView markedTextRange];
UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
if (!position || !selectedRange)
{
if (toBeString.length > maxLength)
{
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
if (rangeIndex.length == 1)
{
textView.text = [toBeString substringToIndex:maxLength];
}
else
{
NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
textView.text = [toBeString substringWithRange:rangeRange];
}
}
}
}
- UITextField去除emoji并且限制输入文字数量
[textField addTarget:self action:@selector(textFiledEditChanged:) forControlEvents:UIControlEventEditingChanged];
- (void)textFiledDidChange:(UITextField *)textField
{
//禁止emoji输入
if (textField.text.length > 0) {
NSString *text = [self disable_emoji:textField.text];
if (![text isEqualToString:textField.text]) {
textField.text = text;
}
}
//后面可以是 限制字数的代码
NSInteger maxLength = 50;
NSString *toBeString = textField.text;
//获取标记文本范围= 临时插入的需要用户确认的文本长度.确认输入以后,变成0
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
if (!position)
{
if (toBeString.length > maxLength)
{
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
if (rangeIndex.length == 1)
{
textField.text = [toBeString substringToIndex:maxLength];
}
else
{
NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
textField.text = [toBeString substringWithRange:rangeRange];
}
}
}
}
2.UITextView去除emoji并且限制输入文字数量
- (void)textViewDidChange:(UITextView *)textView{
if (textView.text.length > 0) {
// 禁止系统表情的输入
NSString *text = [self disable_emoji:textView.text];
if (![text isEqualToString:textView.text]) {
NSRange textRange = [textView selectedRange];
textView.text = text;
[textView setSelectedRange:textRange];
}
}
//限制输入字数
//获取高亮文本范围= 临时插入的需要用户确认的文本长度.确认输入以后,变成0
//获取高亮部分
UITextRange *selectedRange = [textView markedTextRange];
UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
if (!position || !selectedRange)
{
if (toBeString.length > maxLength)
{
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
if (rangeIndex.length == 1)
{
textView.text = [toBeString substringToIndex:maxLength];
}
else
{
NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
textView.text = [toBeString substringWithRange:rangeRange];
}
}
}
}
下面是disable_emoji方法的代码
- (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;
}
网友评论