使用 PDFKit 删除高亮注释
Removing highlight annotation with PDFKit
我使用 swift 和 PDFKit 成功地向 pdf 添加了高亮注释,但我无法弄清楚如何让用户再次删除高亮。
用户可以正常select文本,然后从UIMenu中选择"Highlight"或"Remove highlight"。
要在 select 编辑文本时自定义 pdfView,我更改了显示的菜单 - 首先删除默认操作:
extension PDFView {
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
}
然后在 viewDidLoad() 中设置我的自定义 UIMenuItems:
let menuItem1 = UIMenuItem(title: "Highlight", action: #selector(highlightSelection(_:)))
let menuItem2 = UIMenuItem(title: "Remove highlight", action: #selector(removeHighlightSelection(_:)))
UIMenuController.shared.menuItems = [menuItem1, menuItem2]
当 select 突出显示时:
@objc func highlightSelection(_ sender: UIMenuItem) {
let selections = pdfViewer.currentSelection?.selectionsByLine()
guard let page = selections?.first?.pages.first else { return }
selections?.forEach({ selection in
let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.color = .yellow
page.addAnnotation(highlight)
})
}
到目前为止一切顺利 - 到目前为止一切正常。突出显示文本并创建注释。
现在我的问题来了:
当我 select 突出显示的文本时,我希望用户能够通过点击 "Remove highlight" 来删除突出显示的注释,但我根本不知道如何只删除隐藏的注释 "behind" selected 文本。
此代码有效,但删除了整个页面上的所有注释:
@objc func removeHighlightSelection(_ sender: UIMenuItem) {
let selections = pdfViewer.currentSelection?.selectionsByLine()
guard let page = selections?.first?.pages.first else { return }
let annotationsToRemove = page.annotations
for annotation in annotationsToRemove {
page.removeAnnotation(annotation)
print("Removed: \(annotation)")
}
}
那么,如何只删除 selected 突出显示注释?
顺便说一句 - 我知道整个菜单的东西并不是真正相关的,但我希望有人在使用高亮注释时会发现这个问题,然后能够使用该部分。
谢谢,
埃米尔.
在此处观察此通知:PDFViewAnnotationHitNotification:https://developer.apple.com/documentation/foundation/nsnotification/name/1504809-pdfviewannotationhit
向通知侦听器添加目标
当按下注释时,return "PDFAnnotationHit" 的 PDFViewAnnotationHitNotification 的 userInfo 中的字典值,您将拥有被按下的确切注释,一旦您拥有此注释,将其删除.
好吧,其实我也是摸索出来的
我决定采用以下方法来删除高亮注释:
1:获取页面上的所有注释。
2:通过检查注释的 bounds.origin 是否在选择范围内来检测哪些注释是当前选择的一部分 。
3:检查是否选择了 "Highlight" 类型的注释 is/are 并仅删除这些注释。
4: 删除注释。
5: 不要忘记在某些时候保存更改。
//Remove highlight annotations that are part of the current selection.
@objc func removeHighlightSelection(_ sender: UIMenuItem) {
let selection = PDFSelection.init(document: pdfViewer.document!)
selection.add(pdfViewer.currentSelection!)
let selectionBounds = selection.bounds(for: pdfViewer.currentPage!)
let annotations = pdfViewer.currentPage?.annotations
let annotationsToDelete = NSMutableArray()
for annotation in annotations! {
let annotationPoint = annotation.bounds.origin
if selectionBounds.contains(annotationPoint) {
annotationsToDelete.add(annotation)
print(annotationsToDelete)
}
}
for annotation in annotationsToDelete {
let onlyHighlight = annotation as! PDFAnnotation
if onlyHighlight.type == "Highlight" {
pdfViewer.currentPage?.removeAnnotation(onlyHighlight)
print("Removed highlight annotation: \(onlyHighlight)")
}
}
}
如果您使用 UISearchBar 来突出显示文本,只需使用这些方法
1- 删除旧注释
func removeAllAnnotations() {
guard let document = self.baseView.document else { return }
for i in 0..<document.pageCount {
if let page = document.page(at: i) {
let annotations = page.annotations
for annotation in annotations {
page.removeAnnotation(annotation)
}
}
}
}
2-添加新注释
func highlight(searchTerms: [String]?)
{
searchTerms?.forEach { term in
let selections = baseView?.document?.findString(term, withOptions: [.caseInsensitive])
selections?.forEach { selection in
for page in selection.pages{
let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.endLineStyle = .square
highlight.color = UIColor.orange.withAlphaComponent(0.5)
highlightsAnotation.append(highlight)
page.addAnnotation(highlight)
}
}
}
}
像这样在 UISearchBarDelegates 中调用这些方法
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
removeAllAnnotations()
highlight(searchTerms: [searchText])
}
我使用 swift 和 PDFKit 成功地向 pdf 添加了高亮注释,但我无法弄清楚如何让用户再次删除高亮。
用户可以正常select文本,然后从UIMenu中选择"Highlight"或"Remove highlight"。
要在 select 编辑文本时自定义 pdfView,我更改了显示的菜单 - 首先删除默认操作:
extension PDFView {
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
}
然后在 viewDidLoad() 中设置我的自定义 UIMenuItems:
let menuItem1 = UIMenuItem(title: "Highlight", action: #selector(highlightSelection(_:)))
let menuItem2 = UIMenuItem(title: "Remove highlight", action: #selector(removeHighlightSelection(_:)))
UIMenuController.shared.menuItems = [menuItem1, menuItem2]
当 select 突出显示时:
@objc func highlightSelection(_ sender: UIMenuItem) {
let selections = pdfViewer.currentSelection?.selectionsByLine()
guard let page = selections?.first?.pages.first else { return }
selections?.forEach({ selection in
let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.color = .yellow
page.addAnnotation(highlight)
})
}
到目前为止一切顺利 - 到目前为止一切正常。突出显示文本并创建注释。
现在我的问题来了:
当我 select 突出显示的文本时,我希望用户能够通过点击 "Remove highlight" 来删除突出显示的注释,但我根本不知道如何只删除隐藏的注释 "behind" selected 文本。
此代码有效,但删除了整个页面上的所有注释:
@objc func removeHighlightSelection(_ sender: UIMenuItem) {
let selections = pdfViewer.currentSelection?.selectionsByLine()
guard let page = selections?.first?.pages.first else { return }
let annotationsToRemove = page.annotations
for annotation in annotationsToRemove {
page.removeAnnotation(annotation)
print("Removed: \(annotation)")
}
}
那么,如何只删除 selected 突出显示注释?
顺便说一句 - 我知道整个菜单的东西并不是真正相关的,但我希望有人在使用高亮注释时会发现这个问题,然后能够使用该部分。
谢谢, 埃米尔.
在此处观察此通知:PDFViewAnnotationHitNotification:https://developer.apple.com/documentation/foundation/nsnotification/name/1504809-pdfviewannotationhit
向通知侦听器添加目标
当按下注释时,return "PDFAnnotationHit" 的 PDFViewAnnotationHitNotification 的 userInfo 中的字典值,您将拥有被按下的确切注释,一旦您拥有此注释,将其删除.
好吧,其实我也是摸索出来的
我决定采用以下方法来删除高亮注释:
1:获取页面上的所有注释。
2:通过检查注释的 bounds.origin 是否在选择范围内来检测哪些注释是当前选择的一部分 。
3:检查是否选择了 "Highlight" 类型的注释 is/are 并仅删除这些注释。
4: 删除注释。
5: 不要忘记在某些时候保存更改。
//Remove highlight annotations that are part of the current selection.
@objc func removeHighlightSelection(_ sender: UIMenuItem) {
let selection = PDFSelection.init(document: pdfViewer.document!)
selection.add(pdfViewer.currentSelection!)
let selectionBounds = selection.bounds(for: pdfViewer.currentPage!)
let annotations = pdfViewer.currentPage?.annotations
let annotationsToDelete = NSMutableArray()
for annotation in annotations! {
let annotationPoint = annotation.bounds.origin
if selectionBounds.contains(annotationPoint) {
annotationsToDelete.add(annotation)
print(annotationsToDelete)
}
}
for annotation in annotationsToDelete {
let onlyHighlight = annotation as! PDFAnnotation
if onlyHighlight.type == "Highlight" {
pdfViewer.currentPage?.removeAnnotation(onlyHighlight)
print("Removed highlight annotation: \(onlyHighlight)")
}
}
}
如果您使用 UISearchBar 来突出显示文本,只需使用这些方法
1- 删除旧注释
func removeAllAnnotations() {
guard let document = self.baseView.document else { return }
for i in 0..<document.pageCount {
if let page = document.page(at: i) {
let annotations = page.annotations
for annotation in annotations {
page.removeAnnotation(annotation)
}
}
}
}
2-添加新注释
func highlight(searchTerms: [String]?)
{
searchTerms?.forEach { term in
let selections = baseView?.document?.findString(term, withOptions: [.caseInsensitive])
selections?.forEach { selection in
for page in selection.pages{
let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.endLineStyle = .square
highlight.color = UIColor.orange.withAlphaComponent(0.5)
highlightsAnotation.append(highlight)
page.addAnnotation(highlight)
}
}
}
}
像这样在 UISearchBarDelegates 中调用这些方法
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
removeAllAnnotations()
highlight(searchTerms: [searchText])
}