WebView添加和编辑文本作为链接objective-C [英] WebView adding and editing text as a link objective-C

查看:127
本文介绍了WebView添加和编辑文本作为链接objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个mac应用程序,它需要有一个与在Apple邮件中添加和编辑邮件中的url相同的功能。

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.

推荐答案

最后,我能够解决问题。请找到以下代码片段:

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;
}

这篇关于WebView添加和编辑文本作为链接objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆