Ios CGContext将文本倒置
我使用CGContext呈现NSAttributed字符串,但是当它呈现文本时,文本是颠倒的:( 这就是我想到的:Ios CGContext将文本倒置,ios,swift,pdfkit,Ios,Swift,Pdfkit,我使用CGContext呈现NSAttributed字符串,但是当它呈现文本时,文本是颠倒的:( 这就是我想到的: override func draw(with box: PDFDisplayBox, in context: CGContext) { UIGraphicsPushContext(context) context.saveGState() let paragraphStyle = NSMutableParagraphStyle(
override func draw(with box: PDFDisplayBox, in context: CGContext) {
UIGraphicsPushContext(context)
context.saveGState()
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes = [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 80.0),
NSAttributedString.Key.foregroundColor: UIColor.red
]
NSAttributedString(string: self.widgetStringValue!, attributes: attributes).draw(in: self.bounds)
context.restoreGState()
UIGraphicsPopContext()
}
我尝试添加以下内容:
context.translateBy(x: 0.0, y: bounds.height)
context.scaleBy(x: 1.0, y: -1.0)
但是,我的屏幕上根本没有显示文本:(
我做错了什么
更新
根据要求,这里是我的完整TextAnnotation类:
class TextAnnotation: PDFAnnotation {
var currentText: String?
override init(bounds: CGRect, forType annotationType: PDFAnnotationSubtype, withProperties properties: [AnyHashable : Any]?) {
super.init(bounds: bounds, forType: annotationType, withProperties: properties)
self.widgetFieldType = PDFAnnotationWidgetSubtype(rawValue: PDFAnnotationWidgetSubtype.text.rawValue)
self.font = UIFont.systemFont(ofSize: 80)
self.isMultiline = true
self.widgetStringValue = "Text Here"
self.currentText = self.widgetStringValue!
}
override func draw(with box: PDFDisplayBox, in context: CGContext) {
UIGraphicsPushContext(context)
context.saveGState()
let pageBounds = bounds
context.translateBy(x: 0, y: pageBounds.height)
context.scaleBy(x: 1, y: -1)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes: [NSAttributedString.Key: Any] = [
.paragraphStyle: paragraphStyle,
.font: UIFont.systemFont(ofSize: 80),
.foregroundColor: UIColor.red
]
widgetStringValue!.draw(in: pageBounds, withAttributes: attributes)
context.restoreGState()
UIGraphicsPopContext()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
在
PDFanRotation
中,您只需添加translateBy(x:y:)
和scaledBy(x:y:)
,如前所述:
override func draw(with box: PDFDisplayBox, in context: CGContext) {
UIGraphicsPushContext(context)
context.saveGState()
context.translateBy(x: 0, y: bounds.height)
context.scaleBy(x: 1, y: -1)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes: [NSAttributedString.Key: Any] = [
.paragraphStyle: paragraphStyle,
.font: UIFont.systemFont(ofSize: 80),
.foregroundColor: UIColor.red
]
widgetStringValue?.draw(in: bounds, withAttributes: attributes)
context.restoreGState()
UIGraphicsPopContext()
}
在
PDFPage
中,您应该使用来获取PDFDisplayBox
中的边界,然后使用translateBy(x:y:)
和scaledBy(x:y:)
,正如您所讨论的:
override func draw(with box: PDFDisplayBox, to context: CGContext) {
UIGraphicsPushContext(context)
context.saveGState()
let pageBounds = bounds(for: box)
context.translateBy(x: 0, y: pageBounds.height)
context.scaleBy(x: 1, y: -1)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes: [NSAttributedString.Key: Any] = [
.paragraphStyle: paragraphStyle,
.font: UIFont.systemFont(ofSize: 80),
.foregroundColor: UIColor.red
]
text.draw(in: pageBounds, withAttributes: attributes)
context.restoreGState()
UIGraphicsPopContext()
}
在我的例子中,我对一个PDFanRotation进行了子类化。关键是在我的draw方法中为translateBy调用使用了正确的y值(这很难理解):
override func draw(with box: PDFDisplayBox, in context: CGContext) {
let text = NSString(string: "Hello!")
UIGraphicsPushContext(context)
context.saveGState()
context.translateBy(x: 0, y: bounds.height + (2 * bounds.minY))
context.scaleBy(x: 1, y: -1.0)
text.draw(in: bounds.offsetBy(dx: 2, dy: 0), withAttributes: attributes)
context.restoreGState()
UIGraphicsPopContext()
}
代码中有一个错误:无法在此行上调用非函数类型“CGRect”的值:让pageBounds=bounds(for:box)我假设您使用的是
PDFPage
,但现在回想起来,情况显然不是这样。我建议您编辑您的问题(a)显示完整的类;以及(b)你是如何使用它的。例如,我猜你正在做一个PDFanRotation
?不管它是什么,请向我们展示你如何创建和使用它的代码。现在回想起来,这个问题有点不清楚。但是上面两个都对我有用,正确地翻转它,所以请帮助我重现你的问题。