10

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

Hemang
  • 26,840
  • 19
  • 119
  • 186
amirfl
  • 1,634
  • 2
  • 18
  • 34
  • Something [here](https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html#//apple_ref/occ/intf/UITextInputTraits)? – Hot Licks Sep 29 '12 at 00:47
  • I couldn't find a solution there. Any specifics? – amirfl Sep 29 '12 at 01:18
  • 5
    I don't understand why this question was "closed as not a real question". Anyway I found the solution. Subclassed UITextField and overided "-(BOOL)canPerformAction:(SEL)action withSender:(id)sender". By returning 'NO' to if (action == @selector(defineSelection:)), the "define" option was removed from the UIMenuController – amirfl Sep 29 '12 at 18:20
  • 3
    There are those who will close a question if they don't know the right answer. – Hot Licks Sep 29 '12 at 18:54
  • This question seems pretty real to me. Thanks for figuring it out. – Jarsen May 18 '13 at 00:08
  • 6
    Seems that with iOS7, the selector is `_define:`. Instead of relying on a specific undocumented selector name, I used a string comparison: `if ([[NSStringFromSelector(action) lowercaseString] rangeOfString:@"define"].location != NSNotFound) { return NO; } else { return [super canPerformAction:action withSender:sender]; }` – pix0r Oct 26 '13 at 10:45

4 Answers4

5

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:
Thomás Pereira
  • 9,589
  • 2
  • 31
  • 34
2

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)

Community
  • 1
  • 1
tmr
  • 1,500
  • 15
  • 22
-1

If you use a NIB, set the "Correction" property of the UITextField to "NO"(Default value is YES).

If you use code, set the "autocorrectionType" as "UITextAutocorrectionTypeNO".

Asciiom
  • 9,867
  • 7
  • 38
  • 57
  • 1
    Again, this does not prevent "Define" from showing up in the menu when selecting the contents of the textField. – amirfl Sep 29 '12 at 02:15
  • I think your question is unclear and we have the wrong idea of what you want to do... You don't want the "Define" option to show up after a long press on the word of interest? The define that performs a lookup in a dictionary? – Stunner Sep 29 '12 at 02:35
-2

Try this:

UITextField* textField = //...;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
Stunner
  • 12,025
  • 12
  • 86
  • 145
  • 1
    I set the "correction" property to 'NO' and also tried the above suggestion in code, but still got the 'define' menu option. – amirfl Sep 29 '12 at 01:16
  • It's in the xib. I also tried your suggestion and set the autocorrectionType to UITextAutocorrectionTypeNo directly in code. i used: UITextField *unitValue = (UITextField *)[cell viewWithTag:1001]; unitValue.autocorrectionType = UITextAutocorrectionTypeNo; – amirfl Sep 29 '12 at 01:45