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