1,標(biāo)簽的創(chuàng)建
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//設(shè)置標(biāo)簽x坐標(biāo):10,y坐標(biāo):20,長:300,寬:100
var label=UILabel(frame:CGRectMake(10,20, 300, 100))
label.text="hangge.com"
self.view.addSubview(label);
}
2,背景顏色和文字顏色的設(shè)置
label.textColor=UIColor.whiteColor() //白色文字
label.backgroundColor=UIColor.blackColor() //黑色背景
3,對齊方式的設(shè)置
1
label.textAlignment=NSTextAlignment.Right //文字右對齊
4,文字陰影的設(shè)置
label.shadowColor=UIColor.grayColor() //灰色陰影
label.shadowOffset=CGSizeMake(-5,5) //陰影的偏移量
5,字體的設(shè)置
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,文字大小自適應(yīng)標(biāo)簽寬度
1
label.adjustsFontSizeToFitWidth=true //當(dāng)文字超出標(biāo)簽寬度時,自動調(diào)整文字大小,使其不被截斷
8,使標(biāo)簽可以顯示多行文字
1
label.numberOfLines=2 //顯示兩行文字(默認(rèn)只顯示一行,設(shè)為0表示沒有行數(shù)限制)
9,設(shè)置文本高亮
//設(shè)置文本高亮
label.highlighted = true
//設(shè)置文本高亮顏色
label.highlightedTextColor = UIColor.greenColor()
10,富文本設(shè)置
//富文本設(shè)置
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))
//設(shè)置字體顏色
attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(),
range: NSMakeRange(0, 3))
//設(shè)置文字背景顏色
attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(),
range: NSMakeRange(3,3))
label.attributedText = attributeString