我想让 Text Widget PDFAnnotation 只读。我试图将 isReadOnly 标志设置为 true,但它似乎没有任何区别。用户在点击注释后仍然可以对其进行编辑。
最佳答案
PDFKit 不支持注释上的 isReadOnly 属性似乎是一个错误/疏忽。但是,我能够通过在文档中的其他注释上添加一个空白注释来解决这个问题。我向 PDF 文档添加了一个 makeReadOnly() 扩展,它对所有注释执行此操作以使整个文档只读。这是代码:
// A blank annotation that does nothing except serve to block user input
class BlockInputAnnotation: PDFAnnotation {
init(forBounds bounds: CGRect, withProperties properties: [AnyHashable : Any]?) {
super.init(bounds: bounds, forType: PDFAnnotationSubtype.stamp, withProperties: properties)
self.fieldName = "blockInput"
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(with box: PDFDisplayBox, in context: CGContext) {
}
}
extension PDFDocument {
func makeReadOnly() {
for pageNumber in 0..<self.pageCount {
guard let page = self.page(at: pageNumber) else {
continue
}
for annotation in page.annotations {
annotation.isReadOnly = true // This _should_ be enough, but PDFKit doesn't recognize the isReadOnly attribute
// So we add a blank annotation on top of the annotation, and it will capture touch/mouse events
let blockAnnotation = BlockInputAnnotation(forBounds: annotation.bounds, withProperties: nil)
blockAnnotation.isReadOnly = true
page.addAnnotation(blockAnnotation)
}
}
}
}
关于iOS PDFKit : make Text Widget PDFAnnotation readonly,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49941644/