如何渲染一个旋转的UIlabel

我目前正在开发一个应用程序,如iPad的Photoshop,当我想要我的图层数组平坦,DrawTextInRect不保留我UILabel的变换( CAAffineTransformMakeRotation )。 任何人有任何想法?

将您的文本绘制到另一个上下文中并获取它的CGImage,然后查看在旋转的上下文中绘制图像是否可以工作(应该)。

编辑:我没有这样做,我相信它会工作。 当你这样做的时候,标签可能不应该被插入到视图中,但是也可能以这种方式工作。 您可能需要尝试:

 UIGraphicsBeginImageContextWithOptions( myLabel.bounds.size, NO, 0); // 0 == [UIScreen mainScreen].scale CGContextRef context = UIGraphicsGetCurrentContext(); [myLabel.layer renderInContext:context]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

将上下文转换为标签的原点,然后将标签的变换应用于上下文。

下面的代码是在Swift中。

 let context = UIGraphicsGetCurrentContext()! context.saveGState() let t = myLabel.transform let translationPt = CGPoint.init(x: myLabel.frame.origin.x, y: myLabel.frame.origin.y) context.translateBy(x: translationPt.x, y: translationPt.y) context.concatenate(t) myLabel.layer.render(in: context) context.restoreGState()