42 lines
1.1 KiB
Swift
42 lines
1.1 KiB
Swift
import UIKit
|
|
|
|
#if canImport(DTCoreText)
|
|
import DTCoreText
|
|
|
|
/// 基于 DTCoreText 的 Core Text 直接绘制视图
|
|
/// 将 DTCoreText 的排版结果直接绘制到 UIView 上,跳过 UITextView 的间接渲染。
|
|
final class RDEPUBTextPageRenderView: UIView {
|
|
var layoutFrame: DTCoreTextLayoutFrame? {
|
|
didSet {
|
|
setNeedsDisplay()
|
|
}
|
|
}
|
|
|
|
var drawOptions: DTCoreTextLayoutFrameDrawingOptions = DTCoreTextLayoutFrameDrawingOptions(rawValue: 1)! {
|
|
didSet {
|
|
setNeedsDisplay()
|
|
}
|
|
}
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
backgroundColor = .clear
|
|
isOpaque = false
|
|
contentMode = .redraw
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func draw(_ rect: CGRect) {
|
|
guard let context = UIGraphicsGetCurrentContext(),
|
|
let layoutFrame else { return }
|
|
|
|
context.saveGState()
|
|
layoutFrame.draw(in: context, options: drawOptions)
|
|
context.restoreGState()
|
|
}
|
|
}
|
|
#endif
|