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

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

本文將演示標籤控件的基礎用法,swift

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

 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 }
相關文章
相關標籤/搜索