IOS UITextField limits the input length, iosuitextfield

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.