iOS 11에 탑재된 PDFKit는 생각보다 대단합니다(하이라이트, 검색).
PDFKit 설정
Main.storyboard에 UIView를 추가하여 전체 화면으로 확장합니다.반을 PDFView로 바꾸다.
ViewController를 pdfView로 연결합니다.
그림에서 보듯이
sample.pdf를 루트 디렉토리에 추가합니다.PDF 표시
import UIKit
import PDFKit
class ViewController: UIViewController {
@IBOutlet weak var pdfView: PDFView!
override func viewDidLoad() {
super.viewDidLoad()
// documentをセットします
pdfView.document = getDocument()
// 背景や表示設定を切り替えることができます
pdfView.backgroundColor = .lightGray
pdfView.autoScales = true
pdfView.displayMode = .singlePageContinuous
pdfView.usePageViewController(true)
}
func getDocument() -> PDFDocument? {
guard let path = Bundle.main.path(forResource: "sample", ofType: "pdf") else {
print("failed to get path.")
return nil
}
let pdfURL = URL(fileURLWithPath: path)
let document = PDFDocument(url: pdfURL)
return document
}
}
잘 나왔어.
시험해 보다
다음은 아래 그림처럼 메뉴에서
Highlight 옵션을 꺼내서 실제적으로 돋보이게 합니다.기본값은 Copy/Select All 두 개입니다.
UIMenuController를 효과적으로 활용할 수 있습니다.
class ViewController: UIViewController {
@IBOutlet weak var pdfView: PDFView!
override func viewDidLoad() {
super.viewDidLoad()
pdfView.document = getDocument()
pdfView.backgroundColor = .lightGray
pdfView.autoScales = true
pdfView.displayMode = .singlePageContinuous
pdfView.usePageViewController(true)
createMenu()
}
private func getDocument() -> PDFDocument? {
guard let path = Bundle.main.path(forResource: "sample", ofType: "pdf") else {
print("failed to get path.")
return nil
}
let pdfURL = URL(fileURLWithPath: path)
let document = PDFDocument(url: pdfURL)
return document
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(highlight(_:)) {
return true
}
return false
}
private func createMenu() {
let highlightItem = UIMenuItem(title: "Highlight", action: #selector(highlight(_:)))
UIMenuController.shared.menuItems = [highlightItem]
}
@objc private func highlight(_ sender: UIMenuController?) {
guard let currentSelection = pdfView.currentSelection else { return }
let selections = 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.endLineStyle = .square
page.addAnnotation(highlight)
}
pdfView.clearSelection()
}
}
검색 결과 강조 표시
class ViewController: UIViewController {
...
func find(text: String) {
let selections = pdfView.document?.findString(text, withOptions: .caseInsensitive)
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.endLineStyle = .square
page.addAnnotation(highlight)
}
}
}
find("hogehoge") 및 강조 표시고광의 헤드셋을 검출해 보다
NotificationCenter.
...
override func viewDidLoad() {
super.viewDidLoad()
...
NotificationCenter.default.addObserver(forName: .PDFViewAnnotationHit, object: nil, queue: nil, using: notified)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
@objc private func notified(notification: Notification) {
print("Highlightがタップされました!")
}
...
샘플 코드 여기 있습니다.https://github.com/ngo275/PDFKitSample
참고 자료
https://developer.apple.com/documentation/pdfkit/pdfannotation
https://developer.apple.com/documentation/pdfkit/pdfdocument
https://developer.apple.com/documentation/uikit/uimenucontroller
Reference
이 문제에 관하여(iOS 11에 탑재된 PDFKit는 생각보다 대단합니다(하이라이트, 검색).), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ngo275/items/bbdc16daa58d2926b77c텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)