我正在尝试选择并复制使用TTTAttributedLabel库显示的文本。我创建了CopyableLabel类以使TTTAttributedLabel可复制,就像你想要使UILabel可复制一样。这工作正常,但它不允许选择文本的一部分/部分,但它复制孔文本。我尝试在canPerformAction中实现select或selectselectAll动作,然后尝试突出显示要复制的文本的所需部分:
class CopyableLabel :TTTAttributedLabel {
override var canBecomeFirstResponder: Bool {
return true
}
override func canPerformAction(_ action: Selector, withSender sender: (Any)?) -> Bool {
return action == #selector(UIResponderStandardEditActions.copy)
}
func select() {
becomeFirstResponder()
let menu = UIMenuController.shared
if !menu.isMenuVisible {
menu.setTargetRect(bounds, in: self)
menu.setMenuVisible(true, animated: true)
}
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.copy) {
return true
}else if action == #selector(UIResponderStandardEditActions.selectAll(_:)){
return true
}
return false
}
}
当我点击选择全部菜单时,应用程序崩溃并显示错误
由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:CopiableLabel selectAll:]:发送到实例的无法识别的选择器