Link inside PDFDocument using NSAttributedString

I have the following code in a prototype application that displays text with a link and presents it in a PDFView.

        let a4SizePageRect = CGRect(x: 0, y: 0, width: 595.2, height: 841.8)
        let renderer = UIGraphicsPDFRenderer(bounds: a4SizePageRect)
        
        let title = "Some Title Link"
        let attributedTitle = NSMutableAttributedString(string: title, attributes: [
            .font: UIFont.boldSystemFont(ofSize: 24),
            .link: "<insert here some correct link>",
            .underlineStyle: NSUnderlineStyle.single.rawValue
        ])
        
        let data = renderer.pdfData { ctx in
            ctx.beginPage()
            attributedTitle.draw(in: a4SizePageRect.insetBy(dx: 25, dy: 25))
        }
        
        pdfView.document = PDFDocument(data: data)

The link only works on iOS 15 versions, but does not work on iOS 16.4 and iOS 17 RC.

Based on the documentation for the property and NSAttributedString, the link attribute should still work.

Has NSAttributedString stopped supporting NSAttributedString.Key.link in recent versions of iOS?

Has NSAttributedString stopped supporting NSAttributedString.Key.link in recent versions of iOS?

No. But there’s been a lot of changes in both Foundation and PDFKit recently, so it’s not 100% clear whether this is a Foundation issue or a PDFKit issue.

If you take your attributed string and put it into some other context, like a text view, does it work? If so, that’s a strong indicator that the Foundation side of this is working fine and you need to focus on PDFKit.

Unfortunately I don’t have a lot of expertise in PDFKit. If this does turn out to live on the PDFKit side of things, my only suggestion is that you open a DTS tech support incident and discuss this with our PDFKit expert.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Link inside PDFDocument using NSAttributedString
 
 
Q