[Xcode 实际操作]四、常用控件-(4)UILabel文本标签的自动换行
本文将演示标签控件的换行功能,
在项目导航区,打开视图控制器的代码文件【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 }