扯犊子的话就不说了,记录一下项目中实际用到的一些限制输入的运用:
1:最常用的限制输入长度;
//输入框添加监听事件
[self.textFieldaddTarget:selfaction:@selector(textFieldChanged:)forControlEvents:UIControlEventEditingChanged];
//实现方法
- (void)textFieldChanged:(UITextField *)textField {
NSString *toBeString = textField.text;
NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage;
if ([lang isEqualToString:@"zh-Hans"]) { //中文输入
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
if (!position) {// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (toBeString.length > 15) {
textField.text = [toBeString substringToIndex:15];
[textField resignFirstResponder];
}
}
}else{
if (toBeString.length > 15) {
textField.text = [toBeString substringToIndex:15];
}
}
}
2.限制空格的输入
//禁止输入空格
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
if(![stringisEqualToString:tem]) {
returnNO;
}
return YES;
}
3.限制表情的输入
//UITextField代理方法
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {// 不让输入表情
if([textField isFirstResponder]) {
if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) {
returnNO;
}
}
//判断键盘是不是九宫格键盘
if([ZZClass isNineKeyBoard:string] ){
returnYES;
}else{
if([ZZClass hasEmoji:string] || [ZZClass stringContainsEmoji:string]){
returnNO;
}
}
return YES;
}
以上三个判断方法如下(我也是网上找到的,自己拿来用顺便做个总结)
/**
* 判断字符串中是否存在emoji
*@paramstring 字符串
*@returnYES(含有表情)
*/
+ (BOOL)stringContainsEmoji:(NSString*)string
{
__block BOOL returnValue =NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
^(NSString*substring,NSRangesubstringRange,NSRangeenclosingRange,BOOL*stop) {
const unichar hs = [substring characterAtIndex:0];
if(0xd800<= hs && hs <=0xdbff) {
if(substring.length>1) {
constunicharls = [substringcharacterAtIndex:1];
constintuc = ((hs -0xd800) *0x400) + (ls -0xdc00) +0x10000;
if(0x1d000<= uc && uc <=0x1f77f) {
returnValue =YES;
}
}
}elseif(substring.length>1) {
constunicharls = [substringcharacterAtIndex:1];
if(ls ==0x20e3) {
returnValue =YES;
}
}else{
if(0x2100<= hs && hs <=0x27ff) {
returnValue =YES;
}elseif(0x2B05<= hs && hs <=0x2b07) {
returnValue =YES;
}elseif(0x2934<= hs && hs <=0x2935) {
returnValue =YES;
}elseif(0x3297<= hs && hs <=0x3299) {
returnValue =YES;
}elseif(hs ==0xa9|| hs ==0xae|| hs ==0x303d|| hs ==0x3030|| hs ==0x2b55|| hs ==0x2b1c|| hs ==0x2b1b|| hs ==0x2b50) {
returnValue =YES;
}
}
}];
returnreturnValue;
}
/**
* 判断字符串中是否存在emoji 限制第三方键盘(常用的是搜狗键盘)的表情
*@paramstring 字符串
*@returnYES(含有表情)
*/
+ (BOOL)hasEmoji:(NSString*)string {
NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", pattern];
BOOLisMatch = [predevaluateWithObject:string];
returnisMatch;
}
/**
判断是不是九宫格
@paramstring 输入的字符
@returnYES(是九宫格拼音键盘)
*/
+ (BOOL)isNineKeyBoard:(NSString*)string
{
NSString*other =@"➋➌➍➎➏➐➑➒";
intlen = (int)string.length;
for(inti=0;i
{
if(!([otherrangeOfString:string].location!=NSNotFound))
returnNO;
}
return YES;
}