0
votes

I have UIWebView in my application. Inside UIWebView I have some structured text. When user taps on some portion of that text it is being highlighted without being selected. That is I monitor touch events using JQuery and if user just taps the text for a short moment I change the background of that text portion to some color using JQuery and CSS, thus highlighting the text without actually selecting it. As the user hasn't actually selected the text UIMenuController is not being shown. That is not the case what I wanted. I want to show UIMenuController upon highlighted text manually. So when user taps the portion of text it should be highlighted and also UIMenuController should be shown at that tap location. How can I actually trigger UIMenuController in UIWebView on specific text portion (which is actually <span> HTML element) without selecting that portion? Thanks for help.

1
Hi, I need exactly the same thing. Could you share with me your solution to this at last?Bowie

1 Answers

0
votes

Load html in web page (which contains contenteditable as true and java script function to get rect of selected text (getRectForSelectedText()))...B using this method u will get frame of selected Text inside UIWebView so that we can present UIMenuController with this frame

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *html = [NSString stringWithFormat:@"<script type=\"text/javascript\">function getRectForSelectedText() {var selection = window.getSelection(); var range = selection.getRangeAt(0); var rect = range.getBoundingClientRect(); return '{{' + rect.left + ',' + rect.top + '}, {' + rect.width + ',' + rect.height + '}}';}</script>\"<html><body><div id=\"content\" contenteditable=\"true\" style=\"font-family: Helvetica\">This is out Rich Text Editing View</div></body></html>"];
    [self.viewWeb loadHTMLString:html baseURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",NSTemporaryDirectory()]]];
}

- (CGRect)cursorRect {
    NSString *str = [self.viewWeb stringByEvaluatingJavaScriptFromString:@"getRectForSelectedText()"];
    return CGRectFromString(str);
}