import UIKit /// 自定义 UITextView,替换系统默认的 UIMenuItem 为自定义操作(拷贝、高亮、批注) final class RDEPUBSelectableTextView: UITextView { /// 选择菜单操作回调 var onSelectionAction: ((RDEPUBAnnotationMenuAction) -> Void)? override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { switch action { case #selector(rd_copy(_:)), #selector(rd_highlight(_:)), #selector(rd_annotate(_:)): return selectedRange.location != NSNotFound && selectedRange.length > 0 default: return false } } @objc func rd_copy(_ sender: Any?) { onSelectionAction?(.copy) } @objc func rd_highlight(_ sender: Any?) { onSelectionAction?(.highlight) } @objc func rd_annotate(_ sender: Any?) { onSelectionAction?(.annotate) } }