admin管理员组文章数量:1122846
I am using PDFKit to create a PDF from a view. My view has some UITextViews with links and I would like those links to be clickable in PDF. That I managed to achieve by using text.draw(at: textViewOrigin, withAttributes: attributes)
method. However all links in PDF are always of standard tint iOS colour and I am struggling to change it to black. Any idea how I can do it?
Slightly simplified code looks like this:
guard let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
fatalError("Cannot find document directory")
}
let url = documentsPath.appendingPathComponent("\(pdfTitle).pdf")
let pdfRenderer = UIGraphicsPDFRenderer(bounds: view.bounds)
hideTextView()
do {
try pdfRenderer.writePDF(to: url, withActions: { context in
context.beginPage()
view.layer.render(in: context.cgContext)
let attributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14),
NSAttributedString.Key.link: "mailto:\(text)",
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,
NSAttributedString.Key.foregroundColor: UIColor.black, // NOT working - it changes colour of text, but colour of link won't change
] as [NSAttributedString.Key : Any]
text.draw(at: textViewOrigin, withAttributes: attributes)
})
} catch {
print("Could not create PDF file: \(error)")
}
I am using PDFKit to create a PDF from a view. My view has some UITextViews with links and I would like those links to be clickable in PDF. That I managed to achieve by using text.draw(at: textViewOrigin, withAttributes: attributes)
method. However all links in PDF are always of standard tint iOS colour and I am struggling to change it to black. Any idea how I can do it?
Slightly simplified code looks like this:
guard let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
fatalError("Cannot find document directory")
}
let url = documentsPath.appendingPathComponent("\(pdfTitle).pdf")
let pdfRenderer = UIGraphicsPDFRenderer(bounds: view.bounds)
hideTextView()
do {
try pdfRenderer.writePDF(to: url, withActions: { context in
context.beginPage()
view.layer.render(in: context.cgContext)
let attributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14),
NSAttributedString.Key.link: "mailto:\(text)",
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,
NSAttributedString.Key.foregroundColor: UIColor.black, // NOT working - it changes colour of text, but colour of link won't change
] as [NSAttributedString.Key : Any]
text.draw(at: textViewOrigin, withAttributes: attributes)
})
} catch {
print("Could not create PDF file: \(error)")
}
Share
Improve this question
asked Nov 21, 2024 at 11:22
ShaluginShalugin
1,1943 gold badges11 silver badges15 bronze badges
1 Answer
Reset to default 0I think I found a workaround:
Draw text without a link attribute:
let text = "https://www.stackoverflow.com" let attributes = [ NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue, NSAttributedString.Key.foregroundColor: UIColor.black, // Set colour here ] as [NSAttributedString.Key : Any] text.draw(at: textViewOrigin, withAttributes: attributes)
Create PDFDocument from the url where PDF is rendered and allow PDFKit add hyperlink automatically:
let pdfDocument = PDFDocument(url: url) let pdfData = pdfDocument?.dataRepresentation()
Text attributes will be those set in Step 1 and thanks to some magic that happens in PDFDocument(url: url) link will be clickable. Then we can share PDF or whatever else was the goal.
本文标签: core graphicsHow to change link colour in PDFKit in SwiftStack Overflow
版权声明:本文标题:core graphics - How to change link colour in PDFKit in Swift? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311429a1934734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论