IOS 9 App Development tutorial Display edit text label text box

Source: Internet
Author: User
Tags uikit
<span id="Label3"></p>IOS 9 App Development tutorial Display edit text label text box iOS9 display, Edit Text<p><p>On ios, you will often see some text displayed. Words are the mouths of these dumb devices. With these words, you can clearly specify the information that these devices want to express. This section focuses on the three views that are used to display and edit text in ios: labels, text boxes, and text views.</p></p>iOS9 read-only Text--tags<p><p>The label view is a read-only text view that displays a small amount of information to the user in the application, as shown in 2.16. The display of the text in this illustration uses the label VIEW. The label view is typically implemented using the Uilabel class.</p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong></strong></p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong>Figure 2.16 Beauty Camera</strong></p></p><p><p>"example 2-6" Below is the effect of displaying a poem through a tabbed view. The code is as Follows:</p></p><p class="a1"><p class="a1"></p></p> <ul> <li><li>Import UIKit</li></li> <li><li>Class Viewcontroller:uiviewcontroller {</li></li> <li><li>Override Func Viewdidload () {</li></li> <li><li>Super.viewdidload ()</li></li> <li><li>Additional setup after loading the view, typically from a nib.</li></li> <li><li>Add a Label object MyTitle</li></li> <li><li>Let Mytitle=uilabel (frame:cgrectmake (88, 100, 198, 58))</li></li> <li><li><strong>mytitle.font=uifont.boldsystemfontofsize () </strong>//set font: Bold</li></li> <li><li><strong>mytitle.textalignment=nstextalignment.center </strong>//set Alignment</li></li> <li><li>mytitle.text= "send a friend"</li></li> <li><li>Self.view.addSubview (mytitle)</li></li> <li><li>Add a Label object verse1</li></li> <li><li>Let Verse1=uilabel (frame:cgrectmake (44, 197, 303, 63))</li></li> <li><li><strong>verse1.font=uifont.systemfontofsize (+) </strong>//set font</li></li> <li><li>Verse1.textalignment=nstextalignment.center</li></li> <li><li>verse1.text= "north of aoyama, White water around the east side." "</li></li> <li><li>Self.view.addSubview (verse1)</li></li> <li><li>Add a Label object Verse2</li></li> <li><li>Let Verse2=uilabel (frame:cgrectmake (44, 268, 303, 63))</li></li> <li><li>Verse2.font=uifont.systemfontofsize (24)</li></li> <li><li>Verse2.textalignment=nstextalignment.center</li></li> <li><li>Verse2.text= "here is a no, the solitary Peng wan." "</li></li> <li><li>Self.view.addSubview (verse2)</li></li> <li><li>Add a Label object verse3</li></li> <li><li>Let Verse3=uilabel (frame:cgrectmake (44, 339, 303, 63))</li></li> <li><li>Verse3.font=uifont.systemfontofsize (24)</li></li> <li><li>Verse3.textalignment=nstextalignment.center</li></li> <li><li>verse3.text= "floating Clouds wandering meaning, the Sunset. "</li></li> <li><li>Self.view.addSubview (verse3)</li></li> <li><li>Add a Label object Verse4</li></li> <li><li>Let Verse4=uilabel (frame:cgrectmake (44, 410, 303, 63))</li></li> <li><li>Verse4.font=uifont.systemfontofsize (24)</li></li> <li><li>Verse4.textalignment=nstextalignment.center</li></li> <li><li>Verse4.text= "waved himself to go, rustling Xiaoxiao. "</li></li> <li><li>Self.view.addSubview (verse4)</li></li> <li><li>}</li></li> <li><li>......</li></li> <li><li>}</li></li> </ul><p><p></p></p><p><p>When you run the program, you see the effect shown in 2.17.</p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong></strong></p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong>Figure 2.17 Running effect</strong></p></p><p class="a2"><p class="a2">Note: The label view is displayed by default in one row, but you can also display the contents of the label as multiple Lines.</p></p><p><p>"example 2-7" The following will display multiple lines of content in the Label. The code is as Follows:</p></p><p class="a1"><p class="a1"></p></p> <ul> <ul> <li>Import UIKit</li> <li>Class Viewcontroller:uiviewcontroller {</li> <li>Override Func Viewdidload () {</li> <li>Super.viewdidload ()</li> <li>Additional setup after loading the view, typically from a nib.</li> <li>Let Verse=uilabel (frame:cgrectmake (20, 191, 333, 196))</li> </ul> </ul><p><p>verse.text= "people like the sun, like the sun, like the sunshine of love, like the sunshine of mercy, the world is everywhere." A warm greeting, a sweet smile, a simple blessing, a small help, a gesture of goodwill, all is the sunshine of our Lives. Why don't we collect them, let it shine on our path, warm our hearts in need of warmth? The heart of the sun more, oneself is a sun, what is the wind and rain to be able to extinguish the sun? "</p></p><p><p></p></p><p class="a1"><p class="a1"></p></p> <ul> <ul> <li>Self.view.addSubview (verse)</li> <li>}</li> <li>......</li> <li>}</li> </ul> </ul><p><p></p></p><p><p>When you run the program, you see the effect shown in 2.18. In the diagram you can see that only one line of text is displayed in the label and not all of the content is Displayed. To solve this problem, iOS provides an attribute numberoflines that displays multiple lines, so that the content that is displayed in the label can be displayed in more than one LINE. In this example, our label content takes 9 lines to display, so add the following in your code:</p></p><p class="a1"><p class="a1"></p></p> <ul> <ul> <li><strong>Verse.numberoflines=9</strong></li> </ul> </ul><p><p></p></p><p><p>When you run the program, you see the effect shown in 2.19.</p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong></strong></p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong>Figure 2.18 Run effect Figure 2.19 Run effect</strong></p></p>Single-line read-write text-text Box<p><p>A text box is a common single-line read-write text view, which is simply an input control, such as a login interface that requires the user to enter a user name and password, as shown in 2.1. Text boxes are generally implemented using UITEXTFIELD.</p></p><p><p>"example 2-8" The following by using a text box to achieve the function of adding accounts in QQ. The following are the Steps:</p></p><p><p>(1) Create a single View application template type project, named Uitextfield.</p></p><p><p>(2) Open the Main.storyboard file and design the main view, as shown in effect 2.20.</p></p><p><p>The views that need to be added and the settings for them are shown in table 2-5.</p></p><p class="a4" style="text-align: center;"><p class="a4" style="text-align: center;"><strong>Table 2-5 settings for a View object</strong></p></p><p class="a0"><p class="a0"><strong><br><br></strong></p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong>Figure 2.20 Effect of the main view</strong></p></p><p><p>(3) Open the Viewcontroller.swift file, write code, The function of this code is to add the Account. The code is as Follows:</p></p><p class="a1"><p class="a1"></p></p> <ul> <li><li>Import UIKit</li></li> <li><li>Class Viewcontroller:uiviewcontroller {</li></li> <li><li>Override Func Viewdidload () {</li></li> <li><li>Super.viewdidload ()</li></li> <li><li>Additional setup after loading the view, typically from a nib.</li></li> <li><li>Add a text box to enter your account</li></li> <li><li>Let Accountnumber=uitextfield (frame:cgrectmake (0, 116, 375, 50))</li></li> <li><li><strong>Accountnumber.borderstyle=uitextborderstyle.roundedrect </strong>//set The border style of the text box</li></li> <li><li><strong>accountnumber.placeholder= "QQ</strong> <strong>number/</strong> <strong>Mobile phone number/</strong> <strong>mailbox "//</strong> Set placeholder for text box</li></li> <li><li>Self.view.addSubview (accountnumber)</li></li> <li><li>Add a text box to enter a password</li></li> <li><li>Let Password=uitextfield (frame:cgrectmake (0, 165.5, 375, 50))</li></li> <li><li>Password.borderstyle=uitextborderstyle.roundedrect</li></li> <li><li>Password.placeholder= "password"</li></li> <li><li>Self.view.addSubview (password)</li></li> <li><li>}</li></li> <li><li>......</li></li> <li><li>}</li></li> </ul><p><p></p></p><p><p>When you run the program, you see the effect shown in 2.21. When the developer taps the text box, the keyboard automatically pops up, as shown in 2.22.</p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong></strong></p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong>Figure 2.21 Run effect figure 2.22 run effect</strong></p></p>Multi-line read-write Text--text View<p><p>Text view is also an input control, unlike a text box, where the text view allows the user to enter multiple lines, as shown in 2.23. In this figure, the string "say something." this area is implemented using text view, where users can write large amounts of text Content. The General text box view is implemented using UITEXTVIEW.</p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong></strong></p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong>Figure 2.23 Writing a log</strong></p></p><p><p>"example 2-9" The following will be used in the text view of QQ to write and publish the Function. The following steps are Described:</p></p><p><p>(1) Create a single View application template type project, named Uitextview.</p></p><p><p>(2) Open the Main.storyboard file and design the main view, as shown in effect 2.24.</p></p><p><p>The views that need to be added and the settings for them are shown in table 2-6.</p></p><p class="a4" style="text-align: center;"><p class="a4" style="text-align: center;"><strong>Table 2-6 settings for a View object</strong></p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong></strong></p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong></strong></p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong>Figure 2.24 Effect of the main view</strong></p></p><p><p>(3) Open the Viewcontroller.swift file, write the code, the function that this code implements is to write to say and publish Function. The code is as Follows:</p></p><p class="a1"><p class="a1"></p></p> <ul> <li><li>Import UIKit</li></li> <li><li>Class Viewcontroller:uiviewcontroller,uitextviewdelegate {</li></li> <li><li>let <strong>wtv=uitextview (frame:cgrectmake (0, 375, 232))</strong></li></li> <li><li><strong>Let Rtv=uitextview (frame:cgrectmake (0, 372, 375, 232))</strong></li></li> <li><li>Let Label=uilabel (frame:cgrectmake (3, 105, 123, 21))</li></li> <li><li>Override Func Viewdidload () {</li></li> <li><li>Super.viewdidload ()</li></li> <li><li>Additional setup after loading the view, typically from a nib.</li></li> <li><li>Self.view.addSubview (wtv)//add Text View</li></li> <li><li>label.text= "say something ..."//set the text content of the label</li></li> <li><li>Label.enabled=false//disable Label</li></li> <li><li>Label.backgroundcolor=uicolor.clearcolor ()</li></li> <li><li>Wtv.delegate=self//delegate to set text view</li></li> <li><li>Self.view.addSubview (label)</li></li> <li><li>Self.view.addSubview (rtv)</li></li> <li><li>Rtv.backgroundcolor=uicolor.clearcolor ()</li></li> <li><li>Rtv.editable=false//disable text View</li></li> <li><li>Rtv.hidden=true//hide text View</li></li> <li><li>}</li></li> <li><li>Listen for text change messages</li></li> <li><li>Func Textviewdidchange (textview:uitextview) {</li></li> <li><li>Determines whether the contents of the text view are empty</li></li> <li><li>If (wtv.text== "") {</li></li> <li><li>label.text= "say Something ..."</li></li> <li><li>}else{</li></li> <li><li>Label.hidden=true</li></li> <li><li>}</li></li> <li><li>}</li></li> <li><li>Hide keyboard</li></li> <li><li>@IBAction func Cancel (sender:anyobject) {</li></li> <li><li><strong>Wtv.resignfirstresponder ()</strong></li></li> <li><li>}</li></li> <li><li>Publish and say, hide the keyboard</li></li> <li><li>@IBAction Func issue (sender:anyobject) {</li></li> <li><li>Rtv.hidden=false</li></li> <li><li>Rtv.text=wtv.text//set text contents of text view</li></li> <li><li><strong>Wtv.resignfirstresponder ()</strong></li></li> <li><li>}</li></li> <li><li>......</li></li> <li><li>}</li></li> </ul><p><p></p></p><p><p>After you run the program, you see the effect shown in 2.25. When the developer taps the text view, the keyboard automatically pops up, as shown in 2.26.</p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong></strong></p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong>Figure 2.25 Run effect Figure 2.26 run effect</strong></p></p><p><p>When the developer enters content in the text view, the string "say something ..." will automatically disappear, as shown in 2.27. When you tap the publish button, the content that you write in the text view appears in another text view, and the keyboard disappears, as shown in 2.28.</p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong></strong></p></p><p class="a0" style="text-align: center;"><p class="a0" style="text-align: center;"><strong>Figure 2.27 Run effect figure 2.28 run effect</strong></p></p><p class="a0"><p class="a0"><strong>This article is selected from:</strong>IOS 9 Application Development Basic Tutorial University bully Internal information, reproduced please indicate the source, respect the technology respect the IT person!</p></p><p><p>IOS 9 App Development tutorial Display edit text label text box</p></p></span>

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.