1 #import "PhoneField.h" 2 #define myNumbers @"0123456789\n" 3 @implementation PhoneField 4 -(instancetype)initWithEncryptionFlag:(BOOL)EncryptionFlag 5 { 6 7 if([super init]) 8 { 9 self.delegate=self; 10 _EncryptionFlag=EncryptionFlag; 11 self.keyboardType=UIKeyboardTypePhonePad; 12 } 13 return self; 14 } 15 16 17 -(instancetype)init 18 { 19 if([super init]) 20 { 21 self.delegate=self; 22 self.keyboardType=UIKeyboardTypePhonePad; 23 } 24 return self; 25 } 26 /* 27 // Only override drawRect: if you perform custom drawing. 28 // An empty implementation adversely affects performance during animation. 29 - (void)drawRect:(CGRect)rect { 30 // Drawing code 31 } 32 */ 33 34 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 35 { 36 NSCharacterSet *cs; 37 38 39 cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet]; 40 NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; 41 BOOL basicTest = [string isEqualToString:filtered]; 42 if (!basicTest) 43 { 44 return NO; 45 } 46 if(range.location>=11) 47 { 48 return NO; 49 } 50 if(_Changed) 51 { 52 _Changed(); 53 } 54 if(_EncryptionFlag) 55 { 56 if(range.location>=3&&range.location<=6) 57 { 58 textField.text=[textField.text stringByAppendingString:@"*"]; 59 60 return NO; 61 } 62 } 63 64 return YES; 65 } 66 /* 67 // Only override drawRect: if you perform custom drawing. 68 // An empty implementation adversely affects performance during animation. 69 - (void)drawRect:(CGRect)rect { 70 // Drawing code 71 } 72 */ 73 74 @end 75
#使用 76 phoneField=[[PhoneField alloc] initWithEncryptionFlag:YES]; 77 phoneField.clearButtonMode = UITextFieldViewModeWhileEditing; 78 phoneField.placeholder=@"请输入手机号"; 79 phoneField.keyboardType=UIKeyboardTypePhonePad; 80 phoneField.returnKeyType=UIReturnKeyNext; 81 phoneField.backgroundColor=[UIColor whiteColor]; 82 phoneField.delegate=self; 83 _phone=@""; 84 phoneField.Changed=^(UITextField *textField,NSRange range,NSString *string) 85 { 86 ws.phone= [ws.phone stringByAppendingString :string]; 87 };
如上代码 封装后 形成自定义的 uitextfield
真机测试下 在ios8中可以
在 ios7中失败了
键盘无法显示或者显示后无法输入任何内容并且卡死了
真机为 iPhone4S 7.1.1系统(该机已摔伤多次)
另一只无伤iPhone4S 8.1.1系统(没有问题)
怀疑是机子问题后把封装代码放回原来类中,问题没有了
现在不知道问题出在哪里 求解
xcode target 调节到7.1 7.0再试试真机调试
为何要调低0.1个版本?理解不能
@风晕: 你试试 不行的话就算了呗 可以的话就解决了. 我也是猜的😄