【问题标题】:CGContext renders text upside downCGContext 将文本倒置
【发布时间】:2019-07-30 16:02:54
【问题描述】:

我正在用 CGContext 渲染一个 NSAttributedString,但是当它渲染时,文本是颠倒的 :(

这是我想出的:

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")
    }

}

【问题讨论】:

    标签: ios swift pdfkit


    【解决方案1】:

    PDFAnnotation 中,您应该只添加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 中,您应该使用bounds(for:) 来获得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()
    }
    

    【讨论】:

    • 代码有错误:Cannot call value of non-function type 'CGRect' on this line: let pageBounds = bounds(for: box)
    • 我以为您使用的是PDFPage,但回想起来,显然不是这样。我建议您编辑您的问题(a)向我们展示完整的课程; (b) 你如何使用它。例如。我猜你在做PDFAnnotation?不管它是什么,向我们展示你如何创建和使用它的代码。回想起来,这个问题有点不清楚。但是以上两种方法都对我有用,可以适当地翻转它,所以请帮助我重现您的问题。
    【解决方案2】:

    在我的例子中,我是一个 PDFAnnotation 的子类。关键是在我的 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()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      相关资源
      最近更新 更多