问题
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