IOS Uitextfield Limit input length

Source: Internet
Author: User

This blog is mainly about restricting the length of the input, I was asked this question a few days ago, said the limit length will be unable to delete the problem, so just a piece sent to show you. TextField indentation, a picture of the background of the matter, I use the leftview here is purely incidental.

All right, the nonsense, the code, very simple, we can see.

Create a TextField and a button first.

#import "ViewController.h" @interface Viewcontroller () <UITextFieldDelegate> {Uitextfield *currenttextfeild; UIButton *touchbutton;}    @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload];        Additional setup after loading the view, typically from a nib. Uitextfield *textfields = [[Uitextfield alloc] Initwithframe:cgrectmake (40)]    ;    Textfields.backgroundcolor = [Uicolor Browncolor];    TextFields.layer.cornerRadius = 5;    Textfields.leftview = [[UIView alloc] Initwithframe:cgrectmake (0, 0, 10, 40)]; Textfields.leftviewmode = uitextfieldviewmodealways;//These two lines are to not let the text too paste TextField left textfields.placeholder = @ "Please enter phone number"    ;    Textfields.delegate = self;    [Self.view Addsubview:textfields];        Currenttextfeild = Textfields;    UIButton *enablebutton = [UIButton buttonwithtype:uibuttontypecustom];    Enablebutton.frame = CGRectMake (self.view.bounds.size.width-15*2, 40); EnAbleButton.layer.cornerRadius = 5;    Enablebutton.backgroundcolor = [Uicolor Graycolor];    [Enablebutton settitle:@ "no content not clickable" forstate:uicontrolstatenormal];    [Enablebutton settitle:@ "can be pressed" forstate:uicontrolstateselected];    [Enablebutton settitle:@ "pressed down" forstate:uicontrolstatehighlighted];    enablebutton.enabled = NO;    [Enablebutton addtarget:self Action: @selector (Btnclick) forcontrolevents:uicontroleventtouchupinside];    [Self.view Addsubview:enablebutton]; Touchbutton = Enablebutton;} -(void) Btnclick {}

//Set TextField agent  

#pragma mark-uitextfielddelegate-(BOOL) textfieldshouldbeginediting: (Uitextfield *) TextField {return YES;} -(void) textfielddidendediting: (Uitextfield *) TextField {}-(BOOL) TextField: (Uitextfield *) TextField SHOULDCHANGEC    Haractersinrange: (nsrange) Range replacementstring: (NSString *) string {//used to determine whether the input can continue,-range.length is to determine whether you can delete    Nsinteger currentlength = textfield.text.length-range.length + string.length;    if (Currentlength > one) {return NO; }//Determine if the button can enable = YES if (currenttextfeild.text && currentTextFeild.text.length > 0 && cur        Rentlength > 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 = = | | currenttextfeild.tag = = 12) {//Phone number touchbutton.enabled = no;    touchbutton.selected = NO;; } return YES;        -(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField {[TextField resignfirstresponder]; return YES;}

Everyone read the code, almost understand.

IOS Uitextfield Limit input length

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.