Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling "Define" in a UITextField

I have a UITextField that displays only numeric values (0-9, ., -). When a user selects the contents of the text field, a menu with "copy","paste" and "define" appears. Since the textfield only displays numerical values, I don't want the "define" option to appear. How do I disable the dictionary "define" option in a UITextField?

Edit: I've solved this and posted the solution below

like image 875
amirfl Avatar asked Sep 29 '12 00:09

amirfl


2 Answers

Swift - iOS 8

You can do it by subclassing UITextField and overriding canPerformAction:WithSender method.

class MyTextFieldWithoutDefine: UITextField {
    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
        if action == "_define:" {
            return false
        }

        return super.canPerformAction(action, withSender: sender)
    }
}

List of all actions:

cut:
copy:
select:
selectAll:
paste:
delete:
_promptForReplace:
_transliterateChinese:
_showTextStyleOptions:
_define:
_addShortcut:
_accessibilitySpeak:
_accessibilitySpeakLanguageSelection:
_accessibilityPauseSpeaking:
makeTextWritingDirectionRightToLeft:
makeTextWritingDirectionLeftToRight:
like image 59
Thomás Pereira Avatar answered Sep 27 '22 19:09

Thomás Pereira


the solution in the question comment area did not work for me (ios8), i got error with:

action == @selector(defineSelection:)

i was able to remove 'define' from edit menu by specifing the options i wanted to include in the menu:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(copy:) ||
        action == @selector(selectAll:)) {
        return true;
    }

    return false;
}

more complete answer at: How to disable copy paste option from UITextField programmatically (thank you serge-k)

like image 28
tmr Avatar answered Sep 27 '22 19:09

tmr