ios - iOS:混合Core Graphics绘制路径和UIKit绘制文本功能

标签 ios nsstring core-graphics drawrect

我在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/

相关文章:

ios - 连接到 XCode 后,iPhone 位置服务显示错误的位置

ios - UITableView 单元格 Imageview 在单击时更改其大小?

ios - 我的 dateFormatter.dateFromString 方法总是返回 nil iOS Swift

Cocoa:如何处理网页中无效的 utf-8 字节

objective-c - 是否可以将 NSMutableAttributedString 与 SKLabelNode 一起使用?

ios - 电影/GIF 背景 iOS 应用

iphone - iOS 5从应用程序发送电子邮件的最简单方法

ios - -[CALayer setNeedsDisplayInRect :] causes the whole layer to be redrawn

ios - 您能否仅将较大的磁盘镜像的较小矩形部分加载到内存中?

ios - 将更新的图像保存到 PhotoKit 时缺少图像元数据