Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

swift - How can I append value if NSAttributedstring contains external link?

Hi I'm trying to get html from a website. It has internal and external links. How can I append string if link is external link. By the way internal links has applewebdata:// and I check those ones as internal. But I couldn't understand how can I detect external links. I want just add something like that ? .

Internal link href: applewebdata:// External link href contains http:// or https://

An example in here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here what you could do:
Enumerate the link attribute.
Check for each value if it's the one you want (the one that starts with "applewebdata://").
Modify either the rest of the string and/or the link (your question is unclear on that part, I made both).

attributedString needs to be mutable (NSMutableAttributedString).

attributedString.enumerateAttribute(.link, in: NSRange(location: 0, length: attributedString.length), options: [.reverse]) { (attribute, range, pointee) in
    if let link = attribute as? URL, link.absoluteString.hasPrefix("applewebdata://") {
        var replacement = NSMutableAttributedString(attributedString: attributedString.attributedSubstring(from: range))

        //Use replaceCharacters(in range: NSRange, with str: String) if you want to keep the same "effects" (attributes)
        replacement.replaceCharacters(in: NSRange(location: replacement.length, length: 0), with: "~~>")

        //Change the link if needed
        let newLink = link.absoluteString + "2"
        replacement.addAttribute(.link, value: newLink, range: NSRange(location: 0, length: replacement.length))

        //Replace
        attributedString.replaceCharacters(in: range, with: replacement)
    }
}

Code for Playground to put before if needed:

let htmlString = "Hello this <a href="http://stackoverflow.com">external link</a> and that's an <a href="applewebdata://myInternalLink">internal link</a> and that's it."
let htmlData = htmlString.data(using: .utf8)!
let attributedString = try! NSMutableAttributedString(data: htmlData,
                                                      options: [.documentType : NSAttributedString.DocumentType.html],
                                                      documentAttributes: nil)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...