你好,歡迎來到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;
}

總結

以上就是這篇文章的全體內容了,願望能對年夜家的進修或許任務帶來必定的贊助,假如有疑問年夜家可以留言交換。

【應用UITextField限制只可輸出中,英文,數字的辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

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