Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7048419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:53:23+00:00 2026-05-28T02:53:23+00:00

When we highlight a text in UIWebView usually the copy, paste, define.. etc appears.

  • 0

When we highlight a text in UIWebView usually the copy, paste, define.. etc appears. How can I intercept this so that when I choose copy I call some other method/do something else. Is this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:53:24+00:00Added an answer on May 28, 2026 at 2:53 am

    You can simply override -copy:

    - (void)copy:(id)sender
    {
        // Do something else here
        return [super copy:sender];
    }
    

    Edit to answer your comment.

    Define is a bit trickier, since it’s private. However, you could implement your own method. Set up the UIMenuController with your desired items.

    UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"Copy" 
                                                      action:@selector(myCopy:)];
    
    UIMenuItem *defineItem = [[UIMenuItem alloc] initWithTitle:@"Define"
                                                        action:@selector(myDefine:)];
    
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:defineItem, copyItem, nil]];
    [defineItem release];
    [copyItem release];
    

    Then you implement those methods.

    As for define, it’s way more complicated.. First, you need to check if the UIReferenceLibraryController has a definition, by overriding -canPerformAction:withSender:

    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender 
    {
        if (action == @selector(myDefine:)) {
            // Make sure we are on iOS5.x
            if (NSClassFromString(@"UIReferenceLibraryViewController")) {
                return [UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:[webView selectedText]];
            }
        }
        // Implement other custom actions here
        return NO;
    }
    

    -selectedText is here a category on UIWebView:

    - (NSString *)selectedText {
        return [self stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
    }
    

    Then you need to implement myDefine:

    - (void)myDefine:(UIMenuController *)menuController 
    {    
        CGRect selectedWordFrame = [webView rectForSelectedText];
    
        UIReferenceLibraryViewController *dict = [[UIReferenceLibraryViewController alloc] initWithTerm:[webView selectedText]];
        UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:dict];
        [popover presentPopoverFromRect:selectedWordFrame
                                 inView:webView
               permittedArrowDirections:UIPopoverArrowDirectionAny
                               animated:YES];
        [popover setDelegate:self];
        [dict release];
    }
    - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController 
    {
        [popoverController release];
    }
    

    Edit to answer you comment again

    -rectForSelectedText is another custom category on UIWebView.

    - (CGRect)rectForSelectedText {
        return CGRectFromString([self stringByEvaluatingJavaScriptFromString:@"getRectForSelectedWord()"]);
    }
    

    What it does is calling a javascript that returns a string that you can convert using CGRectFromString(), it looks something like this:

    function getRectForSelectedWord() {
        var selection = window.getSelection();
        var range = selection.getRangeAt(0);
        var rect = range.getBoundingClientRect();
        return "{{" + rect.left + "," + rect.top + "}, {" + rect.width + "," + rect.height + "}}";
    }
    

    Check this page to learn how to inject javascript into UIWebView.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a Google chrome extension so that I can highlight text
I know that I can highlight text and its automatically copied, but is there
Can someone help me with a javascript function that can highlight text on a
How to select or highlight text in java script on mousemove. can anybody provide
I want to highlight text in a specific position. Something like this: highlight with
In flex, how can I highlight text/label? I have text and a small image
This is what I do: Go into visual mode Highlight some text Press y
I am trying to highlight a part of the text on my website. This
I would like know whether we can highlight text (colors) of already created PDF
Firefox has this nice feature, highlight text and then view selection source. Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.