import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //建立Label並設置位置 let label = UILabel(frame: CGRect(x: 150,y:250 , width: 100, height: 100)) //設置文本內容 label.text = "文本內容" //添加到視圖中 self.view.addSubview(label) //設置文字顏色及背景顏色 label.textColor = UIColor.green label.backgroundColor = UIColor.orange //文字對其方式 label.textAlignment = .right //右對齊 //文字陰影設置 label.shadowColor = UIColor.gray//灰色陰影 label.shadowOffset = CGSize(width: 2,height: 2) //字體及大小設置 label.font = UIFont.systemFont(ofSize: 18)//自定義字體 label.font = UIFont.systemFont(ofSize:17)//系統字體 //內容文字過長的省略方式 label.lineBreakMode = .byClipping//超出部分裁剪 label.lineBreakMode = .byCharWrapping//以字母結尾換行 label.lineBreakMode = .byTruncatingHead//將頭部超出部分省略號代替 label.lineBreakMode = .byTruncatingTail//將尾部超出部分省略號代替 label.lineBreakMode = .byWordWrapping//以單詞結尾換行 label.lineBreakMode = .byTruncatingMiddle//將中間部分超出部分省略號代替 //文字大小自適應標籤高度 label.adjustsFontSizeToFitWidth = true//當文字超出標籤寬度時,自動調整文字大小,使其不被截斷 //設置顯示行數 label.numberOfLines = 2//當numberOfLine爲0時行數不受限制 //設置文本高亮 label.isHighlighted = true //設置文本高亮顏色 label.highlightedTextColor = UIColor.red //富文本設置 let attributedString = NSMutableAttributedString(string: "for Development") attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "Farah", size: 18)!,range: NSRange(location: 0, length: 5)) attributedString.addAttribute(NSAttributedString.Key.strokeWidth, value: 3, range: NSMakeRange(6, 9)) attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range:NSRange(location: 16, length: 3)) label.attributedText = attributedString } }