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
}