[Xcode10 实际操作]四、常用控件-(3)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         //创建一个位置在(20,100),尺寸为(280,80)的显示区域
 9         let rect = CGRect(x: 20, y: 100, width: 280, height: 80)
10         //设置标签对象的显示区域
11         let label = UILabel(frame: rect)
12         //设置标签对象在默认情况下,显示的文字内容
13         label.text = "Hello, Xcode and Swift!"
14         
15         //创建一个指定大小的字体对象
16         let font = UIFont(name: "Arial", size: 24)
17         //设置标签对象文字的字体和大小
18         label.font = font
19         
20         //设置标签文字的字体颜色为浅灰色
21         label.shadowColor = UIColor.lightGray
22         //设置标签文字的阴影,在横向和纵向上的偏移距离
23         label.shadowOffset = CGSize(width: 2, height: 2)
24         
25         //设置标签文字的对齐方式为右对齐
26         label.textAlignment = NSTextAlignment.right
27         //设置标签文字的颜色为紫色
28         label.textColor = UIColor.purple
29         //设置标签的背景颜色为黄色
30         label.backgroundColor = UIColor.yellow
31         
32         //将标签对象,添加到当前视图控制器的根视图
33         self.view.addSubview(label)
34     }
35 
36     override func didReceiveMemoryWarning() {
37         super.didReceiveMemoryWarning()
38         // Dispose of any resources that can be recreated.
39     }
40 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10014715.html