一僵闯、基本使用
UITextView等脂、UIWebView淋叶、UITextField 默認(rèn)是支持UIMenuController的
而UILable是不支持UIMenuController的爆雹,需要手動(dòng)實(shí)現(xiàn)臼闻。
UIMenuController的展現(xiàn)需要基于一個(gè)View視圖,其交互則需要基于其所在View視圖的Responder,使View成為第一響應(yīng)者隧膘。在界面展示UIMenuController 必須需要實(shí)現(xiàn)兩個(gè)方法
01、成為第一相應(yīng)響應(yīng)者
override var canBecomeFirstResponder: Bool{
return true
}只有成為第一響應(yīng)者寺惫,才能觸發(fā)02方法的執(zhí)行
02疹吃、是否可以接收某些菜單的某些交互操作(剪切、復(fù)制西雀、粘貼萨驶、刪除等)
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return true
}
1.1、UILable實(shí)現(xiàn)編輯菜單
- UIMenuController
class MyLable: UILabel {
override var canBecomeFirstResponder: Bool{
return true
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.cut(_:)) || action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) {
return true
}
return false
}
override func cut(_ sender: Any?) {
// 將自己的文字復(fù)制到粘貼板
self.copy(sender)
self.text = nil
}
override func copy(_ sender: Any?) {
let pasteboard = UIPasteboard.general
pasteboard.string = self.text
}
override func paste(_ sender: Any?) {
let pasteboard = UIPasteboard.general
self.text = pasteboard.string
}
override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = true
let tapgesture = UITapGestureRecognizer(target: self, action: #selector(tapGestureClick(gesture:)))
self.addGestureRecognizer(tapgesture)
}
@objc private func tapGestureClick(gesture:UITapGestureRecognizer){
self.becomeFirstResponder()
if #available(iOS 16.0, *){
let menuConfiguration = UIEditMenuConfiguration(identifier: nil, sourcePoint:CGPoint(x:self.frame.size.width/2 , y: self.frame.height))
menuConfiguration.preferredArrowDirection = .up
let menuInteraction = UIEditMenuInteraction(delegate: nil)
self.addInteraction(menuInteraction)
menuInteraction.presentEditMenu(with: menuConfiguration)
}else if #available(iOS 13.0, *){
let menuContoller = UIMenuController.shared
guard menuContoller.isMenuVisible == false else{return}
menuContoller.showMenu(from: self, rect: self.bounds)
}else{
// targetRect: MenuController需要指向的矩形框
// targetView: targetRect會(huì)以targetView的左上角為坐標(biāo)原點(diǎn)
let menuContoller = UIMenuController.shared
guard menuContoller.isMenuVisible == false else{return}
menuContoller.setMenuVisible(true, animated: true)
menuContoller.setTargetRect(self.frame, in: self.superview!)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
- UIMenuController 自定義
class MyLable: UILabel {
override var canBecomeFirstResponder: Bool{
return true
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(mydelete){
return true
}
return false
}
override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = true
let tapgesture = UITapGestureRecognizer(target: self, action: #selector(tapGestureClick(gesture:)))
self.addGestureRecognizer(tapgesture)
}
@objc private func tapGestureClick(gesture:UITapGestureRecognizer){
self.becomeFirstResponder()
let menuContoller = UIMenuController.shared
if #available(iOS 13.0, *) {
menuContoller.showMenu(from: self, rect: self.bounds)
} else {
menuContoller.setMenuVisible(true, animated: true)
menuContoller.setTargetRect(self.frame, in: self.superview!)
}
let menu = UIMenuItem(title: "我的刪除", action:#selector(mydelete))
menuContoller.menuItems = [menu]
}
@objc func mydelete(){
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}