ios - 如何从pdf文件中删除注释
问题描述
我正在添加注释,但无法删除添加的注释。例如:当我搜索“Tubes”这个词时,它会标记每个“t”字母。
示例图像:
我只想注释“管”字。
以及如何删除我添加的所有注释。我该如何解决这些问题?
这是代码:
var pdfView: PDFView!
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
return highlight(searchTerms: [searchText])
}
public func highlight(searchTerms: [String]?)
{
var highlight = PDFAnnotation()
searchTerms?.forEach { term in
let selections = pdfView?.document?.findString(term, withOptions: [.forcedOrdering])
selections?.forEach { selection in
selection.pages.forEach { page in
highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.endLineStyle = .square
highlight.color = UIColor.yellow.withAlphaComponent(0.5)
page.addAnnotation(highlight)
}
}
}
}
感谢所有帮助:)
解决方案
您可以定义一个函数来删除所有注释,如下所示,并在searchBar
'stextDidChange
方法中调用它:
func removeAllAnnotations() {
guard let document = pdfView.document else { return }
for index in 0..<document.pageCount {
if let page = document.page(at: index) {
let annotations = page.annotations
for annotation in annotations {
page.removeAnnotation(annotation)
}
}
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
removeAllAnnotations()
return highlight(searchTerms: [searchText])
}
推荐阅读
- deep-learning - 是否可以用层替换卷积神经网络中的损失函数?
- python - 如何从 omsnx 图中选择子图?
- ios - SwiftUI:将多个 BindableObjects 放入环境
- android - 如何转换作为字符串的日期并在android中分配一些格式
- python - 基于具有序列的列条目转置数据帧
- r - 仅在闪亮的应用程序中准备好表格输出时才显示框
- arrays - 滚动时如何在tableview中添加20 -20行的循环?
- plsql - 具有联合的 harcode 行
- postgresql - 相当于 PostgreSQL 中的 UNIQUE IDENTIFIER
- php - 为什么php不能打印mysql数据库中的数据?