在drawRect中旋转NSString [英] Rotating a NSString in drawRect

查看:102
本文介绍了在drawRect中旋转NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想旋转位于指定位置的NSString,以便从下向上读取。

I want to rotate my NSString that's at a set location so that it reads from bottom to up.

我知道问题出在CGContextTranslateCTM,但我我不确定该如何解决。如果我对此行发表评论,我会在需要的地方看到我的文字,只是不会旋转。这必须将我的NSString移到不可见的地方。

I know the issue lies in the CGContextTranslateCTM, but I'm not sure how to fix it. If I comment this line, I see my text where I want it, just not rotated. This must be moving my NSString to where it is not visible.

有什么想法?

这是我尝试过的:

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值,但它们位于屏幕外,我看不到它们。

The code above is drawing my NSString values, but they are being positioned off screen where I can not see them.

此代码实际上是将它们显示在屏幕上,只是不在正确的位置。

This code is actually getting them on screen, just not in the right place.

X和Y都是计算出的距离。

X and Y are both calculated distances.

- (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°),您可以执行以下操作:

If you want to rotate a UILabel vertically (90°) you can do :

[_myLabel setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];

UILabel的结果:

The result for a UILabel :

这篇关于在drawRect中旋转NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆