1,標簽的創建
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//設置標簽x坐標:10,y坐標:20,長:300,寬:100
var label=UILabel(frame:CGRectMake(10,20, 300, 100))
label.text="hangge.com"
self.view.addSubview(label);
}
2,背景顏色和文字顏色的設置
label.textColor=UIColor.whiteColor() //白色文字
label.backgroundColor=UIColor.blackColor() //黑色背景
3,對齊方式的設置
1
label.textAlignment=NSTextAlignment.Right //文字右對齊
4,文字陰影的設置
label.shadowColor=UIColor.grayColor() //灰色陰影
label.shadowOffset=CGSizeMake(-5,5) //陰影的偏移量
5,字體的設置
label.font = UIFont(name:"Zapfino", size:20)
6,文字過長時的省略方式
label.lineBreakMode=NSLineBreakMode.ByTruncatingTail //隱藏尾部并顯示省略號
label.lineBreakMode=NSLineBreakMode.ByTruncatingMiddle //隱藏中間部分并顯示省略號
label.lineBreakMode=NSLineBreakMode.ByTruncatingHead //隱藏頭部并顯示省略號
label.lineBreakMode=NSLineBreakMode.ByClipping //截去多余部分也不顯示省略號
7,文字大小自適應標簽寬度
1
label.adjustsFontSizeToFitWidth=true //當文字超出標簽寬度時,自動調整文字大小,使其不被截斷
8,使標簽可以顯示多行文字
1
label.numberOfLines=2 //顯示兩行文字(默認只顯示一行,設為0表示沒有行數限制)
9,設置文本高亮
//設置文本高亮
label.highlighted = true
//設置文本高亮顏色
label.highlightedTextColor = UIColor.greenColor()
10,富文本設置
//富文本設置
var attributeString = NSMutableAttributedString(string:"welcome to hangge.com")
//從文本0開始6個字符字體HelveticaNeue-Bold,16號
attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!,
range: NSMakeRange(0,6))
//設置字體顏色
attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(),
range: NSMakeRange(0, 3))
//設置文字背景顏色
attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(),
range: NSMakeRange(3,3))
label.attributedText = attributeString