Objective-C: Show dictionary definition on UITextField

By chance i founded this tricks that permit to show a “Dictionary definition” for the selected word in a UITextField on a hidden menu.

Menu contains these actions:

  • cut
  • copy
  • select
  • select all
  • paste
  • suggest
  • definition
  • indent left
  • indent right

It’s a cool tricks, that show an hidden menu with a lot of options, that i never see before!

Watch this:

And this is the dictionary definition for pizza on simulator:

How?

Subclass UITextField and override:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  return [super canPerformAction:action withSender:sender];
}

Instead, if you want to block one or more actions from menu, add sel_isEqual

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  if ( sel_isEqual(action, @selector(paste:)) ) return NO;
  if ( sel_isEqual(action, @selector(copy:)) ) return NO;
  // [...] etc etc

  return [super canPerformAction:action withSender:sender];
}

that’s all!

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.