在drawRect中旋转NSString
我想旋转我的NSString在一个设置的位置,以便从底部向上读取。
我知道问题出在CGContextTranslateCTM,但我不知道如何解决它。 如果我评论这一行,我会看到我想要的文字,而不是旋转。 这必须将我的NSString移动到不可见的地方。
思考?
以下是我所尝试的:
NSString * xString = [NSString stringWithFormat:@"%@", self.xStringValue]; CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]); CGContextSaveGState(UIGraphicsGetCurrentContext()); CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, 0); CGContextRotateCTM(UIGraphicsGetCurrentContext(), M_PI/2); [xString drawInRect:(CGRectMake(x, y, width, height) withFont:self.Font]; CGContextRestoreGState(UIGraphicsGetCurrentContext());
编辑:
上面的代码是绘制我的NSString值,但他们正在屏幕上的位置,我看不到他们。
这个代码实际上是让他们在屏幕上,而不是在正确的地方。
X和Y都是计算的距离。
- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGAffineTransform transform = CGAffineTransformMakeRotation(-90.0 * M_PI/180.0); CGContextConcatCTM(context, transform); CGContextTranslateCTM(context, -rect.size.height-x, rect.size.height-y); CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]); [xString drawInRect:CGRectMake(x, y, w, h) withFont:self.textFont]; CGContextRestoreGState(context); }
编辑三:
解:
CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGContextTranslateCTM(context, point.x, point.y); CGAffineTransform textTransform = CGAffineTransformMakeRotation(-1.57); CGContextConcatCTM(context, textTransform); CGContextTranslateCTM(context, -point.x, -point.y); CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor whiteColor] CGColor]); [string drawInRect:CGRectMake(x, y, width, height) withFont:font]; CGContextRestoreGState(context);
如果你想垂直旋转一个UILabel
(90°),你可以这样做:
[_myLabel setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
UILabel的结果是: