0

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.

4

1 に答える 1

0

Webページにhtmlをロードします(これには、選択したテキストの長方形を取得するためのtrueおよびjavaスクリプト関数が含まれています(getRectForSelectedText()))...このメソッドを使用すると、選択したテキストのフレームがUIWebView内に取得されるため、UIMenuControllerを表示できますこのフレーム

- (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);
}
于 2013-12-09T13:55:17.523 に答える