WebView adding and editing text as a link objective-C

谁都会走 提交于 2020-01-06 15:50:13

问题


I am creating a mac application and it requires to have a functionality the same as that is available in the Apple mail for adding and editing the urls in the mail.

If I do the below, I get the problem that it does not detects the selected text as link

DOMRange *selectedDomRange = [self.cannedResponseWebView selectedDOMRange];
DOMNode *commonNode = [selectedDomRange commonAncestorContainer];
DOMNode *parentNode = [commonNode parentNode];
NSString *linkDisplay = [[[parentNode attributes] getNamedItem:@"href"] nodeValue];

Could someone help me out with this. Please let me know if anyone is not able to understand the issue.


回答1:


Finally I was able to solve the problem. Please find the below code snippet:

- (NSString *)webViewContainURLString:(WebView *)webView {
    DOMNode *selectedNode = [[[webView selectedDOMRange] commonAncestorContainer] parentNode];
    DOMNode *anchorNode = [self nodeContaingAnchorNode:selectedNode];
    NSString *urlString = [[[anchorNode attributes] getNamedItem:@"href"] nodeValue];

    return urlString;
}

- (DOMNode *)containingAnchorNode:(DOMNode *)selectedNode {
    DOMNode *startingNode = selectedNode;
    DOMNode *finalNode = nil;

    if (selectedNode) {
        while (startingNode) {
            if ([[startingNode attributes] getNamedItem:@"href"]) {
                finalNode = startingNode;
                break;
            }

            startingNode = [startingNode parentNode];
        }
    }

    return finalNode;
}


来源:https://stackoverflow.com/questions/39807402/webview-adding-and-editing-text-as-a-link-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!