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

[Xcode10 實際操作]四、常用控件-(3)UILabel文本標簽的使用

idt eight memory can height err warning cal super

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

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

[Xcode10 實際操作]四、常用控件-(3)UILabel文本標簽的使用