This article outlines
1. Introduction
2. Introduction to TextField Controls
3. Introduction to TextView controls
4. Opening and closing of the keyboard
5. Turn off and big open keyboard notifications
6. Type of keyboard
Details 1. Introduction
As with label, TextField and TextView are also text-class controls that can edit text content. But TextField and TextView more than a label to use a keyboard, in addition TextField and TextView also each have a trust agreement. By learning the content, you can make the TextField and TextView into the editing state, the keyboard will slide out from the bottom of the screen, click the Return button to close the keyboard.
2. Introduction to TextField Controls
In the Uikit framework, the TextField control is created by the Uitextfield class. In addition, it has a corresponding Uitextfielddelegate delegation protocol. Delegates can help respond to events. TextField inherits the Uicontrol, is subordinate to the real "control", and TextView inherits the Uiscrollview. The placeholder property can be set as a hint, such as Textfield.placeholder = @ "Enter name"; The text is light gray at run time and disappears when there is an input action. Set the Clearbuttonmode property to determine the timing of the clear button after the TextField. For example mytextfield.clearbuttonmode = uitextfieldviewmodewhileediting;
3. Introduction to TextView controls
TextView is a control that can display and edit multiple lines of text, created by the Uitextview class. It also has a corresponding Uitextviewdelegate trust protocol, and we can use delegates to respond to events.
4. Opening and closing of the keyboard
Once controls such as TextField and TextView are in the editing state, the system will intelligently eject the keyboard without any additional action. But turning off the keyboard is not as good as opening the keyboard, we need to implement it in code.
First, let's understand why the keyboard doesn't turn off automatically. These controls become "first responders" when TextField or TextView are in the editing state. To turn off the keyboard, give up the "first responder" identity. In iOS, an event travels along the responder chain from one responder to the next, and if one of the responders does not respond to the event, the event is passed down again.
As the name implies, "first responder" is the first in the responder chain, and the "performance" after the different controls become "first responders" is inconsistent. Controls such as input types such as TextField and TextView will appear on the keyboard, and the keyboard will not close until we let them discard their "first responder" identities. To achieve this, you need to invoke the Resignfirstresponder method of the Uiresponder class, which is typically called when you click the return key or background view of the keyboard. To implement the return key for clicking on the keyboard, call this method, we usually put in their respective trust agreement to achieve:
1 #pragmaMark-uitextfielddelegate2-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField3 {4 [TextField Resignfirstresponder];5 returnYES;6 }7 8 #pragmaMark-uitextviewdelegate9-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) TextTen { One if([Text isequaltostring:@"\ n"]) { A [TextView Resignfirstresponder]; - returnNO; - } the returnYES; -}
To implement a click on the background view, call this method, using the view's Touchesbegan: Touch event to implement the hidden keyboard, when clicking on the view area will trigger this event:
1 -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *)event { 2 [Super touchesbegan:touches Withevent:event]; 3 [Self.view Endediting:yes]; 4 }
In addition, if there are many controls in the interface, or if the control is positioned closer to the bottom of the screen, the control is likely to be blocked by the pop-up keyboard, which can be resolved by adding the Uiscrollview control.
5. Turn off and turn on the keyboard notification
When you turn the keyboard off and on, the iOS system issues the following broadcast notifications: Uikeyboarddidhidenotification (when turned off) and Uikeyboarddidshownotification (on).
When using the broadcast notification, you need to be aware of the appropriate time to register and dismiss the notification, the following example in Viewcontroller in the Viewwillappear: method to register the notification, in the Viewwilldisappear: method to dismiss the notification.
1- (void) Viewwillappear: (BOOL) Animated2 {3 [Super viewwillappear:animated];4 5 //Sign up for keyboard notifications6[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboarddidshow:) Name: UikeyboarddidshownotificationObject: nil];7 8 //Register for keyboard Hide notifications9[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboarddidhide:) Name: UikeyboarddidhidenotificationObject: nil];Ten } One A- (void) Viewwilldisappear: (BOOL) Animated - { - //unblock keyboard Notifications the[[Nsnotificationcenter Defaultcenter] removeobserver:self name:uikeyboarddidshownotificationObject: nil]; - - //To unlock a keyboard hide notification -[[Nsnotificationcenter Defaultcenter] removeobserver:self name:uikeyboarddidhidenotificationObject: nil]; + } - +- (void) Keyboarddidshow: (Nsnotification *) Notif A { atNSLog (@"Keyboard Open"); - } - -- (void) Keyboarddidhide: (Nsnotification *) Notif - { -NSLog (@"Keyboard Hide"); in}
6. Type of keyboard
Controls with input actions have three properties that are used to set keyboard-related properties. For example, the system provides 11 types of keyboard, choose a different keyboard type, will pop up on iOS different keyboard. And you can set the return key text, if the control is entered in the query criteria, we can set the return key text to go or search, the next step is to find the action. Finally, you can set the appearance of the keyboard. It is important to note that these settings are specific to a control that has an input action. Examples of the code are:
1 // set the type of keyboard (enumeration value) 2 Mytextfield.keyboardtype = Uikeyboardtypedefault; 3 4 // text that sets the return key of the keyboard (enumeration value) 5 Mytextfield.returnkeytype =Uireturnkeydone; 6 7 // set the appearance of the keyboard (enumeration values) 8 Mytextfield.keyboardappearance=uikeyboardappearancedark;
TextField and TextView