IOS basics: 08 text and keyboard, and ios Basics
IOS basic articles 08 text and keyboard
Directory:
1. Get involved
2. TextField
3. TextView
4. Enable and disable the keyboard
5. Notification of enabling/disabling the keyboard
6. Types of keyboards
7. Try again
1. Get involved
Like Label, TextField and TextView are also text controls that allow you to edit text content.
You can edit the control content by code, double-click the control, and Text attribute in the property checker. However, TextField and TextView have one more keyboard than Label.
In addition, TextField and TextView each have a delegation protocol.
Therefore, we separate TextField and TextView from the previous Label.
The following example shows how to use TextField and TextView controls:
1 import UIKit 2 3 class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {4 5 override func viewDidLoad () {6 super. viewDidLoad () 7} 8 9 override func didReceiveMemoryWarning () {10 super. didReceiveMemoryWarning () 11} 12 13}
We also need to assign the delegate object ViewController to the delegate attribute delegate of the TextView and TextField controls, which can be implemented through code or Interface Builder designer.
Here, we use the designer for allocation. The specific implementation process is as follows:
Open the storyboard file in the designer, right-click the TextField control, and a shortcut menu is displayed, as shown in.
Drag the small circle behind Outlet-delegate to the View Controller on the left:
The responder chain is uploaded from the first responder to the next responder. If one of the responders does not respond to the event, the event will be passed down again.
As the name suggests, the "First responder" is the first one in the responder chain. The "performance" of different controls after becoming the "First responder" is not the same.
For input controls such as TextField and TextView, the keyboard is closed only when we make these controls give up their "First responder" identity.
To discard the "First responder" identity, call the resignFirstResponder method in the UIResponder class.
This method is usually triggered when you click the return key or background view in the lower right corner of the keyboard. In this case, you can click return to close the keyboard.
To implement this operation, you can use the Commission Protocol of TextField and TextView.
The related implementation code is in the ViewController file, as follows:
1 import UIKit 2 3 class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {4 5 override func viewDidLoad () {6 super. viewDidLoad () 7} 8 9 override func didReceiveMemoryWarning () {10 super. didReceiveMemoryWarning () 11} 12 13 // delegate to discard the first responder 14 // UITextField delegate Method 15 func textFieldShouldReturn (textField: UITextField)-> Bool {16 textField. resignFirstResponder () 17 return true18} 19 20 // delegate to give up the first responder 21 // UITextView delegate Method 22 func textView (textView: UITextView, shouldChangeTextInRange range: nsange, replacementText: string)-> Bool {23 if text = "\ n" {24 textView. resignFirstResponder () 25 return false26} 27 return true28} 29 30}
In the above Code, textFieldShouldReturn: the method defined in the UITextFieldDelegate delegation protocol is called when the user clicks the keyboard.
Among them, the 16th line of code is to discard the first responder, thus disabling the keyboard.
Similarly, textView: shouldChangeTextInRange: replacementText: The method is provided by the UITextViewDelegate delegate Protocol and called when the user clicks the keyboard.
In addition, if there are many widgets in the interface and the active widget is close to the bottom of the screen, the widget may be blocked by the pop-up keyboard.
Now, you can add the UIScrollView control to solve the problem. Let's talk about it later.
The final result is as follows:
1 override func viewWillAppear (animated: Bool) {2 super. viewWillAppear (animated) 3 4 // register the keyboard to notify 5 NSNotificationCenter. defaultCenter (). addObserver (self, selector: "keyboardDidShow", name: UIKeyboardDidShowNotification, object: nil) 6 7 // register the keyboard hide notification 8 nsicationicationcenter. defaultCenter (). addObserver (self, selector: "keyboardDidHide", name: UIKeyboardDidShowNotification, object: nil) 9} 10 11 override func viewWillDisappear (animated: Bool) {12 super. viewWillDisappear (animated) 13 14 // release the keyboard notification 15 nsnotifcenter center. defaultCenter (). removeObserver (self, name: UIKeyboardDidShowNotification, object: nil) 16 17 // remove the keyboard hide notification 18 NSNotificationCenter. defaultCenter (). removeObserver (self, name: UIKeyboardDidShowNotification, object: nil) 19} 20 21 func keyboardDidShow (notification: NSNotification) {22 NSLog ("enable keyboard ") 23} 24 25 func keyboardDidHide (notification: NSNotification) {26 NSLog ("keyboard off") 27}
The registration notification is executed in viewWillAppear: method, and the release notification is executed in viewWillDisppear: method.
KeyboardDidShow: it is sent when a message is opened on the keyboard. keyboardDidHide: it is sent when the keyboard is closed.
6. Types of keyboards
The keyboard we saw earlier is the default system type.
In iOS, open the property checker of the control with the input action. You can find that there are 11 options in the Keyboard Type drop-down box, which indicates the Type of Keyboard in the 11 ,, we can select as needed:
7. Try again
There are not many things, so you can practice more.
Learn to give a few examples and try again. Open those keyboards to see what it looks like.
Finally, I wish you a happy learning experience. Haha.