1、UILabel的創建
1.1、Label基本應用
1)label的基本應用
代碼示例:
//1.UILabel的基本屬性
let normalLabel = UILabel.init()
//1.1 frame
normalLabel.frame = CGRect(x: 10, y: 50, width: 250, height: 50)
//1.2 backgroundColor
normalLabel.backgroundColor = UIColor(red: 0.3,green: 0.5,blue: 0.5,alpha: 1)
//1.3 textAlignment
normalLabel.textAlignment = .Center
//1.4 lineBreakMode(每行斷行的方式)
normalLabel.lineBreakMode = .ByWordWrapping
//1.5 font
normalLabel.font = UIFont.systemFontOfSize(25)
//1.6 text
normalLabel.text = "This is a normalLabel";
//1.7 textColor
normalLabel.textColor = UIColor.blueColor()
//1.8 添加到view
view.addSubview(normalLabel)
2)帶有陰影效果的label
代碼示例:
//2.帶有陰影的Label
let shadowLabel = UILabel.init(frame: CGRect(x: 10, y: 100, width: 300, height: 50))
shadowLabel.text = "This is a shadowLabel"
shadowLabel.textColor = UIColor.blackColor()
//2.1 陰影顏色
shadowLabel.shadowColor = UIColor.blueColor()
//2.2 陰影的范圍
shadowLabel.shadowOffset = CGSize(width: 1.0, height: 2.0)
view.addSubview(shadowLabel)
3)帶有多屬性文本的label
代碼示例:
//3 多屬性文本
let mutableAttributeLabel = UILabel.init(frame: CGRect(x: 10, y:200, width: 300, height: 50))
//3.1 設置多屬性字符串
let attriStr = NSMutableAttributedString.init(string: "This is a mutableAttributeLabel")
//3.2 添加修改屬性的內容和位置(此處可以修改字體、文字顏色、陰影等)
attriStr.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(25), range: NSMakeRange(0, 3))
//3.3 highlighted
mutableAttributeLabel.highlighted = true
//3.4 highlightedTextColor
mutableAttributeLabel.highlightedTextColor = UIColor.blackColor();
mutableAttributeLabel.attributedText = attriStr
view.addSubview(mutableAttributeLabel)
4)自適應高度的label
代碼示例:
//4.自適應高度的label
let autoSizeLabel = UILabel.init()
autoSizeLabel.frame = CGRect(x: 10, y: 300, width: 300, height: 0)
autoSizeLabel.text = "This is a autoSizeLabel This is a autoSizeLabel This is a autoSizeLabel This is a autoSizeLabel This is a autoSizeLabel This is a autoSizeLabel"
autoSizeLabel.textColor = UIColor.blackColor()
//4.1 numberOfLines(此值為0說明label可以多行)
autoSizeLabel.numberOfLines = 0
autoSizeLabel.lineBreakMode = .ByCharWrapping
//4.2.1自適應高度(此方法必須設置寬度的最大值)
// var rect = CGRect()
// let labelRect = CGRect(x: 50, y: 300, width: 300, height: 50)
// rect = autoSizeLabel.textRectForBounds(labelRect, limitedToNumberOfLines: 0)
// autoSizeLabel.frame = rect
//4.2.2.自適應高度(推薦這種方法)
autoSizeLabel.sizeToFit()
view.addSubview(autoSizeLabel)
5)傾斜的label
代碼示例:
//5.傾斜的label
let obliqueLabel = UILabel.init(frame: CGRect(x: 10, y: 500, width: 200, height: 50))
obliqueLabel.text = "This a obliqueLabel"
//5.1 transform (rotation:傾斜)傾斜程度
obliqueLabel.transform = CGAffineTransformMakeRotation(0.5)
view.addSubview(obliqueLabel)
1.2 帶有菜單欄(UIMenuController)的Label
實現步驟:
1.將UILabel的userInteractionEnabled屬性設置為true
2.給UILabel添加手勢
代碼示例:
self.userInteractionEnabled = true
self.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(labelDidTouch)))
3.使UILabel成為第一響應者并在手勢的響應事件中創建UIMenuController
4.自定義要顯示的MenuItem和相應的響應事件
代碼示例:
self.becomeFirstResponder()
let menuControl = UIMenuController.sharedMenuController()
//設置菜單里可見
menuControl.menuVisible = true
//添加菜單欄顯示的item
menuControl.menuItems = [
UIMenuItem.init(title: "fork", action: #selector(forkFunc)),
UIMenuItem.init(title: "star", action: #selector(starFunc)),
UIMenuItem.init(title: "watch", action: #selector(watchFunc))
]
menuControl.setTargetRect(self.bounds, inView: self)
5.調用override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool
函數來決定顯示的item
代碼示例:
let actionBool = (action == #selector(forkFunc))||(action == #selector(watchFunc))||(action == #selector(starFunc))
if actionBool{
return true
}else{
return false
}
6.重寫override func canBecomeFirstResponder() -> Bool
函數
代碼示例:
override func canBecomeFirstResponder() -> Bool {
return true
}