[Xcode10 實際操作]四、常用控制元件-(4)UILabel文字標籤的自動換行
阿新 • • 發佈:2018-11-25
本文將演示標籤控制元件的換行功能,
在專案導航區,開啟檢視控制器的程式碼檔案【ViewController.swift】
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 }