你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 使用UITextField限制只可輸入中,英文,數字的方法

使用UITextField限制只可輸入中,英文,數字的方法

編輯:IOS開發綜合

前言

本文主要介紹使用UITextField限制只可輸入中,英文,數字,我們可以使用NSPredicate正則表達式可以過濾,下面看看詳細的步驟方法

首先設置UItextField的代理

實現如下方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

 if ([self isInputRuleAndNumber:string] || [string isEqualToString:@""]) {
  return YES;
 }
 return NO;
}

然後添加事件,因為選擇輸入法聯想的詞.是不會經過textField:shouldChangeCharactersInRange:replacementString:方法的

 [textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];

實現:

- (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;
  }
 }
}

再來實現限制:

pattern中,輸入需要驗證的通過的字符

     小寫a-z

     大寫A-Z

     漢字\u4E00-\u9FA5

     數字\u0030-\u0039

- (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;
}

字符限制

#define kMaxLength 20;
-(NSString *)getSubString:(NSString*)string
{
 NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
 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;
}

總結

以上就是這篇文章的全部內容了,希望能對大家的學習或者工作帶來一定的幫助,如果有疑問大家可以留言交流。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved