前言:
UITextField其实研究下发现有许多细微的问题,比如监控text的的值。
有可能你会说,用KVO或者用代理,相比较之下,代理比较靠谱。
要用代理的话,我们必须要明白下面这个方法是什莫意思:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
输入字符时,会调用这个方法。通过这个方法可以达到过滤掉键盘输入的文字超长的效果。确实,这个在你输入英文的时候,确实比较靠谱,每次你输入一个字母或者数字的时候都会触发,用来处理或是屏蔽一些特殊字符的。
问题:但是,如果你用中文输入法,你在输入拼音的时候,他会进入这个代理方法,等你去选择你需要的汉字后,他就显示在了textField上面,这也没错,问题是他不会触发上面这个方法。
在这里,我们研究的只是UITextField只允许输入中,英文,数字。
思维导图
示例代码
#import "ViewController.h"
#define kMaxLength 20
@interface ViewController ()<UITextFieldDelegate>
/**
输入框
*/
@property (weak, nonatomic) IBOutlet UITextField *inPutTextField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[_inPutTextField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
NSLog(@"%@-",_inPutTextField.text);
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([self isInputRuleAndNumber:string] || [string isEqualToString:@""]) {
return YES;
}
return NO;
}
#pragma mark - 触发事件
- (void)textFieldChanged:(UITextField *)textField
{
>
NSString *toBeString = textField.text;
NSString *lastString;
if(toBeString.length>0)
lastString=[toBeString substringFromIndex:toBeString.length-1];
>
if (![self isInputRuleAndNumber:toBeString]&&[self hasEmoji:lastString]) {
textField.text = [self disable_emoji:toBeString];
return;
}
NSString *lang = [[textField textInputMode] primaryLanguage];
if([lang isEqualToString:@"zh-Hans"]) {
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
if(!position) {
NSString *getStr = [self getSubString:toBeString];
if(getStr && getStr.length > 0) {
textField.text = getStr;
}
}
} else{
NSString *getStr = [self getSubString:toBeString];
if(getStr && getStr.length > 0) {
textField.text= getStr;
}
}
NSLog(@"%zd",textField.text.length);
}
#pragma mark - 谓词条件限制
/**
pattern中,输入需要验证的通过的字符
小写a-z
大写A-Z
汉字\u4E00-\u9FA5
数字\u0030-\u0039
@param str 要过滤的字符
@return YES 只允许输入字母和汉字
*/
- (BOOL)isInputRuleAndNumber:(NSString *)str {
NSString *pattern = @"[a-zA-Z\u4E00-\u9FA5\\u0030-\\u0039]";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:str];
return isMatch;
}
/*
*实现判断是否是Emoji
*/
- (BOOL)hasEmoji:(NSString*)str{
NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:str];
return isMatch;
}
- (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;
}
/*
* 限制字符长度为 20
*/
-(NSString *)getSubString:(NSString*)string
{
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);//ios手机端通过后台获取含有中文时,出现乱码
NSData* data = [string dataUsingEncoding:encoding];
NSInteger length = [data length];
if (length > kMaxLength) {
NSData *data1 = [data subdataWithRange:NSMakeRange(0, kMaxLength)];
NSString *content = [[NSString alloc] initWithData:data1 encoding:encoding];
if (!content || content.length == 0) {
data1 = [data subdataWithRange:NSMakeRange(0, kMaxLength - 1)];
content = [[NSString alloc] initWithData:data1 encoding:encoding];
}
return content;
}
return nil;
}
@end
参考文章:
1. 字符串编码转换(GBK) NSString, char, NSData相互转化
2. UITextField只允许输入中,英文,数字
3. 一篇关于监控UITextField的软文
对于本篇文章,是我学习参考文章2所获,不知道是否完善,如有建议,请提出!