I am trying to select and than copy text displayed using the TTTAttributedLabel library. I created the CopyableLabel class to make the TTTAttributedLabel copyable, like when you want to make a UILabel copyable. This works fine but it do not allow to select a portion/part of text but it copy the hole text. I tried to implement the select or selectselectAll action in canPerformAction to then try to highlight to desired portion of text to copy:
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
}
}
When I click on the select All menu, the app crashes with the error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: CopiableLabel selectAll:]: unrecognized selector sent to instance