Вот что вы могли бы сделать:
Перечислите атрибут ссылки.
Проверьте для каждого значения, если это то, что вам нужно (то, которое начинается с «applewebdata: //»).
Измените остальную часть строки и / или ссылку (ваш вопрос неясен в этой части, я сделал оба).
attributedString
должен быть изменяемым (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)
}
}
Код для игровой площадки, чтобы поставить перед, если необходимо:
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)