我在UIView扩展类的drawrect方法中使用以下上下文来绘制路径和字符串。
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -rect.size.height);
绘制路径我用
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0f);
CGContextSetLineWidth(context, 1);
CGContextBeginPath(context);
CGContextMoveToPoint(context, origin.x, origin.y);
CGContextAddLineToPoint(context, currentX, origin.y);
......
CGContextStrokePath(context);
画文字我用
CGContextSetLineWidth(context, 2.0);
[self.title drawInRect:CGRectMake(100, 100, 200, 40) withFont:font];
我正确地获取了路径,但文本却朝上了!如果删除CGContextScaleCTM和CGContextTranslateCTM,则路径会颠倒!有人可以帮我解决这个问题吗?
最佳答案
在绘制路径之前保存之前的上下文,然后再还原它:
CGContextRef context = UIGraphicsGetCurrentContext();
// save the original context
CGContextSaveGState(context);
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -rect.size.height);
// draw path
// restore the context
CGContextRestoreGState();
// draw text
那应该做。
关于ios - iOS:混合Core Graphics绘制路径和UIKit绘制文本功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17832218/