iOS Series Basics 08 Text with keyboard

Source: Internet
Author: User

iOS Series Basics 08 Text with keyboard

Directory:

1. Pull the Duzi

2. TextField

3. TextView

4. Opening and closing of the keyboard

5. Turn on/off keyboard notifications

6. Type of keyboard

7. Finally, two more words

1. Pull the Duzi

As with label, TextField and TextView are also text-class controls that can edit text content.

In the context of control content editing, all three can be implemented by code, double-clicking the control, and the Text property in the property inspector, but TextField and TextView use a keyboard more than label.

In addition, TextField and TextView each have a commission agreement.

Therefore, we introduce TextField and TextView separately from the previous label.

Below, we pass a case, and you show the usage of the TextField and TextView controls:

These include two labels, a TextField, and a textview.

The function of this case is to show the user the editing of personal information, TextField display nickname, TextView display introduction.

When TextField and TextView enter the editing state, the keyboard slides out of the bottom of the screen and clicks the "return" key in the lower right corner of the keyboard to turn off the keyboard.

2. TextField

In the Uikit framework, the TextField control is created by the Uitextfield class.

In addition, it has a corresponding Uitextfielddelegate class delegation protocol. Delegates can help us respond to event handling.

TextField inherits the Uicontrol, is subordinate to the real "control", and TextView inherits the Uiscrollview.

Below we use the single View application template to create a project for demonstration.

Open the Main.storyboard design interface, drag two label controls to the interface from the object library, and set their text to "nickname:" and "Introduction:", respectively. Under "nickname:" Put a TextField:

Open the TextField property inspector and enter "Please enter a nickname ..." in the placeholder (placeholder) attribute as the hint text:

When running, the text of the placeholder is light gray, and the text disappears automatically when there is an input action. Similar to the placeholder property of the <input> tag in HTML.

You can also clear the content text entered in TextField by using the Purge button (Clear button) behind TextField:

The results are as follows:

 

3. TextView

TextView is a control that can display and edit multiple lines of text, created by the Uitextview class.

The TextView control has a corresponding Uitextviewdelegate delegate protocol, and we can respond to events with delegates.

Go back to the Interface Builder design interface and place a TextView control under "Introduction:":

In the Viewcontroller code of the view controller you need to implement the Uitextfielddelegate and Uitextviewdelegate delegate protocol, the relevant code is as follows:

1 Import UIKit2 3 classViewcontroller:uiviewcontroller,uitextfielddelegate, uitextviewdelegate {4 5     Overridefunc viewdidload () {6 super.viewdidload ()7     }8 9     Overridefunc didreceivememorywarning () {Ten super.didreceivememorywarning () One     } A  -}

We also need to assign the delegate object Viewcontroller to the delegate properties delegate of the TextView and TextField controls, which can be implemented either through code or interface Builder designer.

Here, we use the designer to assign, the concrete implementation process is as follows:

Open the storyboard storyboard file in the designer, right-click the TextField control, and Pop up the shortcut menu like.

Use your mouse to drag the small circle behind Outlet-delegate to the view controller on the left:

In the same way, drag the small circle behind the TextView control outlet-delegate to the view controller on the left:

After dragging them, and then opening the right-click menu, they will find that their outlet-delegate have been assigned with the View controller:

The final results are as follows:

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... Come out is out, how to shut it down? No, we can only implement it by code.

First, let's understand why the keyboard doesn't turn off automatically.

When TextField or TextView are in the editing state, these controls become "first responders." To turn off the keyboard, discard the identity of the "first responder".

In iOS, an event travels along a responder chain from the first responder to the next responder, 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 hungry in the responder chain, and the "performance" after the different controls become "first responders" is not the same.

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" identity.

To abandon the "first responder" identity, you need to call the Resignfirstresponder method in the Uiresponder class.

This method is usually triggered when you click the return key or the background view in the lower right corner of the keyboard, this case uses the return key to turn off the keyboard.

To do this, you can use the TextField and TextView to implement the delegation protocol.

The relevant implementation code is in the Viewcontroller file, as follows:

1 Import UIKit2 3 classViewcontroller:uiviewcontroller,uitextfielddelegate, uitextviewdelegate {4 5     Overridefunc viewdidload () {6 super.viewdidload ()7     }8 9     Overridefunc didreceivememorywarning () {Ten super.didreceivememorywarning () One     } A      -     //abandonment of the first responder through a delegate implementation -     //Uitextfield Delegate Method theFunc Textfieldshouldreturn (Textfield:uitextfield)Bool { - Textfield.resignfirstresponder () -         return true -     } +  -     //abandonment of the first responder through a delegate implementation +     //Uitextview Delegate Method AFunc TextView (Textview:uitextview, Shouldchangetextinrange range:nsrange, Replacementtext text:string)Bool { at         iftext=="\ n" { - Textview.resignfirstresponder () -             return false -         } -         return true -     } in  -}

In the preceding code, the Textfieldshouldreturn: Method is a method defined in the Uitextfielddelegate delegate protocol that is called when the user taps the keyboard.

Where the 16th line of code is to discard the first responder, thus closing the keyboard.

With this analogy, the TextView:shouldChangeTextInRange:replacementText: Method is a method provided by the Uitextviewdelegate delegate protocol, which is also called when the user taps the keyboard.

In addition, if there are many controls in the interface, the position of the live control is closer to the bottom of the screen, and the control is likely to be blocked by the pop-up keyboard.

At this point, you can add the Uiscrollview control to solve, this we later article.

The final effect is as follows:

5. Turn on/off keyboard notifications

When you turn the keyboard off and on, the iOS system issues the following broadcast notifications: Uikeyboarddidhidenotification and Uikeyboarddidshownotification.

When using broadcast notifications, you need to be aware of when to register and dismiss notifications, while the relevant code in Viewcontroller is as follows:

1     Overridefunc viewwillappear (animated:bool) {2 super.viewwillappear (animated)3         4         //Sign up for keyboard notifications5Nsnotificationcenter.defaultcenter (). Addobserver (Self, selector:"Keyboarddidshow", Name:uikeyboarddidshownotification,Object: nil)6         7         //Register for keyboard Hide notifications8Nsnotificationcenter.defaultcenter (). Addobserver (Self, selector:"Keyboarddidhide", Name:uikeyboarddidshownotification,Object: nil)9     }Ten      One     Overridefunc viewwilldisappear (animated:bool) { A super.viewwilldisappear (animated) -          -         //unblock keyboard Notifications theNsnotificationcenter.defaultcenter (). Removeobserver (self, name:uikeyboarddidshownotification,Object: nil) -          -         //To unlock a keyboard hide notification -Nsnotificationcenter.defaultcenter (). Removeobserver (self, name:uikeyboarddidshownotification,Object: nil) +     } -      + func keyboarddidshow (notification:nsnotification) { ANSLog ("Keyboard Open") at     } -      - func keyboarddidhide (notification:nsnotification) { -NSLog ("keyboard off") -}

The registration notice is performed in the Viewwillappear: method, and the release notification is performed in the Viewwilldisppear: method.

Keyboarddidshow: Message issued when the keyboard is open, Keyboarddidhide: message when the keyboard is closed.

6. Type of keyboard

The keyboard we saw earlier is the system default type.

In iOS, you open the property inspector for a control that has input actions, and you can see that the drop-down box for the keyboard type has 11 options that represent the type of keyboard in 11, and we can choose as needed:

Choosing a different keyboard type will bring up different keyboards on iOS, with the styles shown in the following illustration:

Of course, a total of 11, you can be interested in a try to see what they look like. Ha ha.

In addition to selecting the appropriate keyboard type for the control, we can also customize the return key text, which depends on the control that has the input action.

If the query condition is entered in the control, we can set the return key text to go live Search, to indicate that the user is going to be searched.

The text setting of the return key

7. Finally, two more words

Not many things, we practice better.

Learn to extrapolate, I think a few examples do try. The keyboards are all open to see what they look like.

Finally, I wish you all a happy learning. Ha ha.

iOS Series Basics 08 Text with keyboard

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.