PDFAnnotation draws text upside down

Hi,


I have the following class handling custom textual pdf annotations in my app:


class PDFSheetTextAnnotation: PDFAnnotation {
override func draw(with box: PDFDisplayBox, in context: CGContext) {
let text = value(forAnnotationKey: .contents) as? NSString
UIGraphicsPushContext(context)
context.saveGState()
var attributes = [
NSAttributedStringKey.font: font as Any,
NSAttributedStringKey.foregroundColor: fontColor as Any,
] as [NSAttributedStringKey : Any]
if value(forAnnotationKey: .color) != nil {
attributes[NSAttributedStringKey.backgroundColor] = color
}
text?.draw(in: bounds, withAttributes: attributes)
context.restoreGState()
UIGraphicsPopContext()
}
}


Unfortunately text is drawn upside down. I tried by flipping the context with:


context.textMatrix = .identity
context.scaleBy(x: 1.0, y: -1.0)
context.translateBy(x: 0, y: bounds.height)


but this seems to push my text out of the view. Looking at the original context transformation via the ctm property gives me the following result:


(lldb) po context.ctm
▿ CGAffineTransform
- a : 2.33217189314749
- b : 0.0
- c : 0.0
- d : 2.33217189314749
- tx : -128.766743135888
- ty : -1661.09549454122


I just can't figure out how to transform the context to get rid of the upside down flipping... any hints?


Thank you so much in advance and happy WWDC!

Arno

I have the similar issue, but after I add the code

CGContextTranslateCTM(context, 0.0, self.bounds.size.height);

CGContextScaleCTM(context, 1.0, -1.0);


The text will disappear, do you know why? Thanks!

PDFAnnotation draws text upside down
 
 
Q