UItextfiledのフォーマット入力

8943 ワード

textFieldフォーマット入力DemonリソースダウンロードUItextfiledのカスタムパッケージ、機能は1を含む.フォーマット(銀行カード番号電話番号フォーマット)2を入力します.入力を入力レコードとして保存することができ、入力時により入力内容が動的に選択された入力レコードを除去することができる.キーボードリスニングとframe変更4.ドロップダウンリストの効果は次のとおりです.
大体の考え方は簡単で、UItextfiledを1つのviewの上に置いて、毎回このviewを呼び出すと使いやすい知識点は1を含む.UItextfiledの基本的な使い方2.通知3.述語検索4.応答チェーン5.get setメソッド6.簡単な論理アルゴリズムZJTextfieldViewは5つのモードを含む
typedef NS_ENUM(NSInteger, ZJTextFieldStyle) { 
        ZJTextfieldStyleNone,//   textfiled         ,         
        ZJTextfieldStyleBank,//               16    19 ,        
        ZJTextfieldStylePhone,//      *** **** ****,        
        ZJTextfieldStyleTableList,//          
        ZJTextFieldStyleAccount//       ,       
};

この種の漏れ出しの方法と属性
@interface ZJTextfieldView : UIView
//textfiled             
@property (nonatomic) ZJTextFieldStyle ZJTextfieldStyle;//           textfiled
@property (nonatomic,strong)UITextField * ZJtextfield;//          
@property (nonatomic,copy)NSString * ZJtext;//          ZJTextfieldStyleTableList      
@property (nonatomic,strong)NSMutableArray * talbeListArray;

//        
- (instancetype)initWithFrame:(CGRect)frame;//- (instancetype)initWithCoder:(NSCoder *)aDecoder;
//                         bank   phone
- (BOOL)ZJtextField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
//                    
- (NSString *)ZJtextFiledViewText:(NSString*)string;
//          
- (void)ZJtextFieldCreateTableviewDidBeginEditing:(UITextField *)textField;
//              plist
- (void)ZJtextFieldWriteTextDidEndEditing:(UITextField *)textField;
//return        
- (BOOL)ZJtextFieldShouldReturn:(UITextField *)textField;

まず、このクラスの使用方法について説明します.
//          
- (void)viewDidLoad {
 [super viewDidLoad];
 ZJTextfieldView * tfv = [[ZJTextfieldView alloc]initWithFrame:CGRectMake(50, 50, 300, 50)];
//   frame tfv.ZJtextfield.borderStyle = UITextBorderStyleRoundedRect;
//tfv                    ZJtextfield ,       UItextfield             textfield        
tfv.ZJTextfieldStyle = ZJTextfieldStyleBank;
//          
tfv.ZJtextfield.placeholder = @"    ZJTextfieldStyleBank"; 
tfv.ZJtextfield.delegate = self;
//           UItextfield             
[self.view addSubview:tfv]; 
self.zjtext = tfv;
//self.zjtext         
}

もちろんここまで書き終わっていませんUItextfiledの4つのエージェントメソッドを実装する必要があります.もちろん、プロセスは簡単です.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
 //          
      if (textField == self.zjtext.ZJtextfield) {
         [self.zjtext ZJtextFieldCreateTableviewDidBeginEditing:textField];
      }
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
//               
      if (textField == self.zjtext.ZJtextfield) {
         [self.zjtext ZJtextFieldWriteTextDidEndEditing:self.zjtext.ZJtextfield];
      }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//                    
       if (textField == self.zjtext.ZJtextfield) { 
          return [self.zjtext ZJtextField:textField shouldChangeCharactersInRange:range replacementString:string];
       }
 return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//         
       if (textField == self.zjtext.ZJtextfield) { 
          return [self.zjtext ZJtextFieldShouldReturn:textField];
      }
 return YES;
}

はい、ここで彼の使い方はもう終わりました.簡単に説明した機能はすべて実現しました.複数のtextfiledが必要なら、4つのエージェントメソッドの中でelse if(text===オブジェクト名){それぞれのエージェントポイントメソッドを呼び出せばいい}次に原理を説明します.
//   
- (instancetype)initWithFrame:(CGRect)frame{
       if (self = [super initWithFrame:frame]) {
          [self addViewTextfiled:frame]; 
          self.backgroundColor = [UIColor greenColor]; 
          self.tableArray = [[NSMutableArray alloc]init]; 
          self.talbeListArray = [[NSMutableArray alloc]init]; 
          self.changeFlage = NO;
       }
 return self;
}
//  textfiled   
- (void)addViewTextfiled:(CGRect)frame{ 
      UITextField * textfield = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
      self.ZJtextfield = textfield;
      textfield.backgroundColor = [UIColor grayColor]; 
      [self addSubview:textfield];
}

以上の実装後,textfiled入力が変化したときにこの方法を呼び出し,異なるモードに従って異なるフォーマットを行う.
#pragma mark                       
- (BOOL)ZJtextField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ 
       if (self.ZJTextfieldStyle == ZJTextfieldStyleBank) {
          return [self bankNumberTextField:textField shouldChangeCharactersInRange:range replacementString:string];
       } 

       if (self.ZJTextfieldStyle == ZJTextfieldStylePhone) { 
          return [self phoneNumberTextField:textField shouldChangeCharactersInRange:range replacementString:string];
       }else if (self.ZJTextfieldStyle == ZJTextfieldStyleTableList){ 
          return NO;
       }else if (self.ZJTextfieldStyle == ZJTextFieldStyleAccount){ 
          NSString * str = [NSString stringWithFormat:@"%@%@",textField.text,string]; 
         [self zjtextfiledCountSearchPredicate:str]; 
         return YES;
       }else{
         return YES; 
       }
}


#pragma mark       
- (BOOL)phoneNumberTextField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ 
      if (textField.text.length >12 && ![string isEqualToString:@""]) {
         return NO;
      }else if ([string isEqualToString:@" "]) { 
         NSLog(@"      "); 
         return NO;
      }else if(![string isEqualToString:@""]){ 
         // NSLog(@"====%@ string = %@",textField.text,string);
         if ((textField.text.length - byteNilFlag)%byteNil == 0 && (textField.text.length - byteNilFlag)/byteNil != 0 && string.length > 0) {
             byteNilFlag = byteNilFlag + byteNil; byteNil = 5; 
             NSMutableString * str = [NSMutableString stringWithFormat:@"%@ ",textField.text]; 
             textField.text = str; 
          }
          NSString * str = [NSString stringWithFormat:@"%@%@",textField.text,string]; 
          [self zjtextfiledCountSearchPredicate:str]; 
          return YES; 
      }else{ 
          //             
          if ([[textField.text substringWithRange:range] isEqualToString:@" "]) { 
              NSRange deleteRange = NSMakeRange(0, textField.text.length - 1);
              NSString * str = [textField.text substringWithRange:deleteRange]; 
              textField.text = str;
          } 
          if (textField.text.length < 3) { 
              byteNil = 3;
              byteNilFlag = 0; 
          } 
          if(textField.text.length <= byteNilFlag){
              byteNilFlag = byteNilFlag - byteNil; 
          }
          NSString * str = [NSString stringWithFormat:@"%@%@",textField.text,string]; 
          [self zjtextfiledCountSearchPredicate:str];
          return YES;
      }
}
#pragma mark       
- (BOOL)bankNumberTextField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ 
   //NSLog();
   if (textField.text.length > 19 + 3 && ![string isEqualToString:@""]){
     return NO;
   }else if ([string isEqualToString:@" "]) {
     NSLog(@"      ");
     return NO; 
   }else if(![string isEqualToString:@""]){ 
    // NSLog(@"====%@ string = %@",textField.text,string); 
       if ((textField.text.length - byteNilFlagBank)%byteNilBank == 0 && (textField.text.length - byteNilFlagBank)/byteNilBank != 0 && string.length > 0) {
          byteNilFlagBank = byteNilFlagBank + byteNilBank; 
          byteNilBank = 5; 
          NSMutableString * str = [NSMutableString stringWithFormat:@"%@ ",textField.text];
          textField.text = str; 
       }
       NSString * str = [NSString stringWithFormat:@"%@%@",textField.text,string];
       [self zjtextfiledCountSearchPredicate:str]; return YES; 
    }else{
    //             
        if ([[textField.text substringWithRange:range] isEqualToString:@" "]) {
            NSRange deleteRange = NSMakeRange(0, textField.text.length - 1); 
            NSString * str = [textField.text substringWithRange:deleteRange]; 
            textField.text = str;
        } 
        if (textField.text.length < 4) { 
            byteNilBank = 4; byteNilFlagBank = 0; 
        } 
        if(textField.text.length <= byteNilFlagBank){ 
            byteNilFlagBank = byteNilFlagBank - byteNilBank; 
        } 
        NSString * str = [NSString stringWithFormat:@"%@%@",textField.text,string]; 
        [self zjtextfiledCountSearchPredicate:str]; 
        return YES; 
    }
}

textFieldフォーマット入力Demonリソースダウンロード