IOS UITextField limits the input length, iosuitextfield
This blog mainly talks about the restriction on the length of input. Someone asked me this question a few days ago, saying that the restriction on the length will fail to be deleted, so I just sent it to you. The indentation of textField, a background image to deal with, I use leftView here is purely incidental.
It's easy to paste code.
// Create a textField and a button first.
# Import "ViewController. h "@ interface ViewController () <UITextFieldDelegate> {UITextField * currentTextFeild; UIButton * touchButton;} @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UITextField * textFields = [[UITextField alloc] initWithFrame: CGRectMake (15, 50, self. view. bounds. size. width-15*2, 40)]; textFields. backgroundColor = [UIColor brownColor]; textFields. layer. cornerRadius = 5; textFields. leftView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 10, 40)]; textFields. leftViewMode = UITextFieldViewModeAlways; // these two rows are used to prevent Text from being too pasted to the left of textField. placeholder = @ "Enter the mobile phone number"; textFields. delegate = self; [self. view addSubview: textFields]; currentTextFeild = textFields; UIButton * enableButton = [UIButton buttonWithType: UIButtonTypeCustom]; enableButton. frame = CGRectMake (15,100, self. view. bounds. size. width-15*2, 40); enableButton. layer. cornerRadius = 5; enableButton. backgroundColor = [UIColor grayColor]; [enableButton setTitle: @ "NO content cannot be clicked" forState: UIControlStateNormal]; [enableButton setTitle: @ "you can press" forState: UIControlStateSelected]; [enableButton setTitle: @ "pressed" forState: UIControlStateHighlighted]; enableButton. enabled = NO; [enableButton addTarget: self action: @ selector (btnClick) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: enableButton]; touchButton = enableButton;}-(void) btnClick {}
// Set the textField proxy
# Pragma mark-UITextFieldDelegate-(BOOL) encode :( UITextField *) textField {return YES;}-(void) textFieldDidEndEditing :( UITextField *) textField {}-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string {// used to determine whether the input can be continued,-range. length is used to determine whether NSInteger currentLength = textField can be deleted. text. length-range. length + string. length; if (currentLength> 11) {return NO;} // determines whether the button can enable = YES if (currentTextFeild. text & currentTextFeild. text. length> 0 & currentLength> 0) {touchButton. enabled = YES; touchButton. selected = YES;} else {touchButton. enabled = NO; touchButton. selected = NO;} if (currentLength <= 0) {touchButton. enabled = NO; touchButton. selected = NO;} return YES;}-(BOOL) textFieldShouldClear :( UITextField *) textField {if (currentTextFeild. tag = 11 | currentTextFeild. tag = 12) {// mobile phone number touchButton. enabled = NO; touchButton. selected = NO;} return YES;}-(BOOL) textFieldShouldReturn :( UITextField *) textField {[textField resignFirstResponder]; return YES ;}
After reading the code, you will almost understand it.