给文本标签UILabel添加长按复制功能

http://www.111cn.net/sj/iOS/104236.htm

http://blog.csdn.net/lrenjun/article/details/12582927

自定义一个可复制的标签类,使其能够响应Touch事件并显示复制菜单。

class CopyLabel: UILabel {
override init(frame: CGRect) {
super.init(frame: frame)
userInteractionEnabled = true
let longPress = UILongPressGestureRecognizer(target: self,
action: #selector(handleLongPress(_:)))
addGestureRecognizer(longPress)
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
} override func canBecomeFocused() -> Bool {
return true
} override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
return action == #selector(NSObject.copy(_:))
} override func copy(sender: AnyObject?) {
let pBoard = UIPasteboard.generalPasteboard()
pBoard.string = self.text
} func handleLongPress(gesture: UILongPressGestureRecognizer) {
self.becomeFirstResponder()
let menu = UIMenuController.sharedMenuController()
menu.setTargetRect(self.frame, inView: self)
menu.setMenuVisible(true, animated: true)
}
}
上一篇:eclipse项目导入androidstudio


下一篇:iOS https认证 && SSL/TLS证书申请