我正在尝试选择并复制使用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:]:无法识别的选择器已发送到实例