当我想在CGContext中绘制一个字符串时,文本会倒置显示。
我知道这个问题已经在这里发布过了,但是没有任何解决方案适用于我。
我确实翻转了上下文:
graphContext.scaleBy(x: 1.0, y: -1.0)
graphContext.translateBy(
x: CGFloat(0.0),
y: CGFloat(-1.0 * graphSize.h))
我也试图对textMatrix进行相同的操作,但这对结果没有任何影响。
完整的项目源代码在这里:dropbox,屏幕截图在这里:screen shot 我不明白为什么它不能正常工作。
谢谢。
我的绘制方法:
override func draw(_ rect: CGRect) {
let graphSize = GSize(
w: Double(self.frame.width),
h: Double(self.frame.height))
rebaseAreas(graphSize: graphSize)
graphContext = UIGraphicsGetCurrentContext()!
graphContext.saveGState()
// Flip the context coordinates, in iOS only
graphContext.scaleBy(x: 1.0, y: -1.0)
graphContext.translateBy(
x: CGFloat(0.0),
y: CGFloat(-1.0 * graphSize.h))
// this has no effect, I can delete it and nothing changes
graphContext.textMatrix = CGAffineTransform(scaleX: 2.0, y: -2.0)
drawBackground(
inRect: graph_drawing_rect,
inContext: graphContext)
drawGuidelines(
vAxis: vAxis,
hAxis: hAxis,
inRect: serie_drawing_rect,
inContext: graphContext)
drawSerie(
points,
inRect: serie_drawing_rect,
inContext: graphContext)
drawTitle(
inRect: graph_drawing_rect,
inContext: graphContext)
drawVAxisLabels(
vAxis: vAxis,
inRect: x_labels_drawing_rect,
inContext: graphContext)
drawHAxisLabels(
hAxis: hAxis,
inRect: y_labels_drawing_rect,
inContext: graphContext)
graphContext.restoreGState()
}
我的drawTitle方法如下:
func drawTitle(
inRect rect: GRect,
inContext context: CGContext) {
// saving the calling state
context.saveGState()
let title = titleLabelText
let lblAttributes = [
NSFontAttributeName: titleFont,
NSForegroundColorAttributeName: title_font_color,
] as [String : Any]
let origin = CGPoint(
x: title_label_position.x * rect.w,
y: title_label_position.y * rect.h)
let drawRect = CGRect(
origin: origin,
size: title.size(attributes: lblAttributes))
title.draw(
in: drawRect,
withAttributes: lblAttributes)
// returning the calling state
graphContext.restoreGState()
}