[Xcode 實際操做]4、經常使用控件-(4)UILabel文本標籤的自動換行

目錄:[Swift]Xcode實際操做html

本文將演示標籤控件的換行功能,swift

在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】app

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4 
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7         // Do any additional setup after loading the view, typically from a nib.
 8         //建立一個位置在(10,100),尺寸爲(300,150)的顯示區域
 9         let rect = CGRect(x: 10, y: 100, width: 300, height: 150)
10         //設置標籤對象的顯示區域
11         let label = UILabel(frame: rect)
12         //而後給標籤對象,設置一段較長的文本內容
13         label.text = "Love you、think of you.love you secretly,love you eagerly,wait ,feel disappointed,try hard,lose,and feel sad,go apart,and recall.these are for sake of you.And I will regret for it."
14         
15         //設置標籤的背景顏色爲橙色
16         label.backgroundColor = UIColor.orange
17         //設置標籤文字的顏色爲紫色
18         label.textColor = UIColor.purple
19         //設置標籤文字的對齊方式爲居中對齊
20         label.textAlignment = NSTextAlignment.center
21         
22         //設置標籤文字的換行方式爲:
23         //以空格爲界,並保留整個單詞(即在換行處不會切割單詞)
24         label.lineBreakMode = NSLineBreakMode.byWordWrapping
25         //設置標籤對象不限制行數
26         label.numberOfLines = 0
27         
28         //將標籤對象,添加到當前視圖控制器的根視圖
29         self.view.addSubview(label)
30     }
31 
32     override func didReceiveMemoryWarning() {
33         super.didReceiveMemoryWarning()
34         // Dispose of any resources that can be recreated.
35     }
36 }
相關文章
相關標籤/搜索