How to Render a rotated UIlabel

旧时模样 提交于 2019-12-25 00:57:21

问题


I'm currently developing an app like photoshop for iPad and when i want flatten my array of layers, DrawTextInRect doesn't preserve the Transform (CAAffineTransformMakeRotation) of my UILabel. Anybody have any idea?


回答1:


Draw your text into another context and get a CGImage of it, then see if drawing the image in your rotated context will work (it should).

EDIT: I have not done this myself, I believe it will work. The label probably should not be inserted into a view when you do this, but it might work that way anyway. You may have to experiment:

UIGraphicsBeginImageContextWithOptions( myLabel.bounds.size, NO, 0); // 0 == [UIScreen mainScreen].scale
CGContextRef context = UIGraphicsGetCurrentContext();

[myLabel.layer renderInContext:context];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();



回答2:


Translate the context to the label's origin, and then apply the label's transform to the context.

The code below is in 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()


来源:https://stackoverflow.com/questions/12686274/how-to-render-a-rotated-uilabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!