PDFDrawingView
PDFDrawingView 是一个轻量级的PDF查看器,内置绘图功能。
如何使用
- 使用PDFKit创建PDF文档。
- 使用CocoaPods导入或从此处下载。
- CocoaPods的示例PodFile如下
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'
source "https://github.com/jrosen081/PDFDrawingView.git"
target 'YOUR_TARGET_ID' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for drawSecure
pod 'DrawingPDF'
end
- 使用构造函数方法。
let pdfDrawer = PDFDrawingView(frame: backgroundView.bounds, document: documentPDF, style: .vertical, delegate: self) //Creates an instance of the view with the PDF being displayed vertically
就这么简单。
包含了一个示例项目,展示了如何使用PDFDrawingView。
特性
- 常规绘图
- 突出显示
- 擦除
- 添加文本框,并能够移动和调整大小。
- 使用套索工具移动不同线条和缩放。
- 与Apple Pencil兼容,可通过力度改变绘制的线条
- PDF可竖着或横着显示。
- 在新PDF中添加绘图。
选择使用工具的方式
- 存在一个结构体,其中包含所有选项。
public struct DrawingKeys{
public static let draw = "draw"
public static let scroll = "scroll"
public static let highlight = "highlight"
public static let text = "text"
public static let erase = "erase"
public static let lasso = "lasso"
}
- 以下是告诉视图如何执行的方式。
pdfDrawer.drawingKey = PDFDrawingView.DrawingKeys.draw //Will have the view draw
- 要更改绘图和高亮显示的颜色,执行以下操作:
pdfDrawer.drawingColor = UIColor.red //Changes the drawing color to red
pdfDrawer.highlightColor = UIColor.yellow //Changes the highlight color to yellow
如何决定PDF的显示方式
- 存在一个枚举包含所有选项。
public enum DrawingStyle {
case vertical
case horizontal
}
- 将样式传入构造函数。
- 默认情况下,如果不提供值,则它将垂直显示PDF。
如何通过绘图创建新的PDF
- PDFDrawingView中有一个方法将返回PDF的数据。
let pdfData = self.pdfDrawer.createPDF()
- 如果想要将其本地保存,请使用内置的PDFDocuments方法。
let updatedDocument = PDFDocument(data: pdfData)
updatedDocument?.write(to: "SAMPLE_PATH")
实现代理协议以获取更多关于PDFDrawingView的信息
代理告诉您以下信息:
- 页面已更改
- 视图已创建
extension DrawViewController: PDFDelegate{
func scrolled(to page: Int) {
self.currentPageNumber = page
}
func viewWasCreated() {
doSomething()
}
}