1. 程式人生 > >[Xcode10 實際操作]四、常用控件-(4)UILabel文本標簽的自動換行

[Xcode10 實際操作]四、常用控件-(4)UILabel文本標簽的自動換行

顏色 方式 sad recreate 位置 控制器 空格 after ppi

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

在項目導航區,打開視圖控制器的代碼文件【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 }

[Xcode10 實際操作]四、常用控件-(4)UILabel文本標簽的自動換行