Uitextfield Basic Knowledge
Uitextfield shows some editable content and has some interaction with the user. For example, when you press return on a virtual keyboard, it is typically associated with a keyboard-hidden event. Most of the Uitextfield states have corresponding methods in the Uitextfielddelegate protocol.
Initialization of Uitextfield and some properties
"' OBJC
Name input field Uitextfield *namefield = [[Uitextfield alloc] Initwithframe:cgrectmake (100, max.)];namefield.tag Namefield.delegate = self;//Default Text Namefield.placeholder = @ "name"; namefield.font = [Uifont systemfontofsize:16.0f]; Namefield.textcolor = [Uicolor blackcolor];//the background picture of the input box (you can also choose to set the background color) Namefield.background = [UIImage imagenamed:@] Textfieldbackgroundimage "];//namefield.backgroundcolor = [uicolor lightgraycolor];//Clear Button Namefield.clearbuttonmode = uitextfieldviewmodealways;//keyboard Type namefield.keyboardtype = Uikeyboardtypedefault; [Self.view Addsubview:namefield]; telephone input field Uitextfield *phonefield = [[Uitextfield alloc] Initwithframe:cgrectmake (30, NAMEFIELD.FRAME.ORIGIN.Y + namefield.bounds.size.height+10, $, ()];p Honefield.tag = 101;phonefield.delegate = self; Phonefield.placeholder = @ "Phone";p honefield.keyboardtype = Uikeyboardtypedecimalpad;phonefield.clearbuttonmode = Uitextfieldviewmodealways; [Self.view addsubview:phonefield];//Mailbox input domain Uitextfield *emailfield = [[Uitextfield alloc] InitwIthframe:cgrectmake (+, PHONEFIELD.FRAME.ORIGIN.Y + PhoneField.bounds.size.height +, max,];emailfield.tag) = 102; Emailfield.delegate = Self;emailfield.placeholder = @ "email"; emailfield.keyboardtype = uikeyboardtypeemailaddress; Emailfield.clearbuttonmode = Uitextfieldviewmodealways; [Self.view Addsubview:emailfield];
```
Uitextfield Hidden Keyboard
1. Click the return of the keyboard to hide the keyboard
This method needs to implement the Uitextfielddelegate protocol in the corresponding. h file file. and add the following method to the. m file
"' OBJC
- (BOOL) Textfieldshouldreturn: (Uitextfield *) TextField {[TextField resignfirstresponder]; return YES;}
```
2. Click the blank space to hide the keyboard
The implementation of this method is mainly to the current view to increase the Click event, not the Click event to increase the corresponding processing method, here is to hide the keyboard, so we can in the Click event corresponding method to let Uitextfield abandon the first responder.
"' OBJC
- (void) Dismisskeyboard {nsarray *subviews = [Self.view subviews]; for (id inputtext in subviews) {if ([Inputtext isKindOf Class:[uitextfield class]] {if ([Inputtext Isfirstresponder]) {[Inputtext resignfirstresponder];}}} }
```
Add a click event to the current view
"' OBJC
UITapGestureRecognizer *dismisskeyboardtap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector ( Dismisskeyboard)]; [Self.view Addgesturerecognizer:dismisskeyboardtap];
```
uitextfield--adding checksums for content
"' OBJC
- (BOOL) Textfieldshouldendediting: (Uitextfield *) textField {switch (textfield.tag) {case 100://name {NSLog (@ ' This is Nam Efield "); Add code to verify the name break; } case 101://phone {NSLog (@ "The is Phonefield");//Add code to verify the phone break;} case 102://email {NSLog (@ "This is Emailfield") ; Add code to verify the email break; }
Default:break; } return YES; }
```