一、基本使用
UITextView、UIWebView、UITextField 默认是支持UIMenuController的
而UILable是不支持UIMenuController的,需要手动实现。
UIMenuController的展现需要基于一个View视图,其交互则需要基于其所在View视图的Responder,使View成为第一响应者。在界面展示UIMenuController 必须需要实现两个方法
01、成为第一相应响应者
override var canBecomeFirstResponder: Bool{
return true
}只有成为第一响应者,才能触发02方法的执行
02、是否可以接收某些菜单的某些交互操作(剪切、复制、粘贴、删除等)
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return true
}
1.1、UILable实现编辑菜单
- 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?) {
// 将自己的文字复制到粘贴板
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会以targetView的左上角为坐标原点
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")
}
}