【问题标题】:Rotating a NSString in drawRect在 drawRect 中旋转 NSString
【发布时间】:2013-10-01 21:18:35
【问题描述】:

我想旋转设置位置的 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);

【问题讨论】:

  • NSString 从 CGContextTranslateCTM 移出视图。不知道如何设置,或者是否有另一种方式来旋转 NSString。
  • 你检查过这个吗? stackoverflow.com/questions/10289898/…
  • 这有点帮助,但仍未在正确的位置绘制。仍在努力。

标签: ios objective-c nsstring rotation drawrect


【解决方案1】:

如果您想垂直旋转 UILabel (90°),您可以这样做:

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

UILabel 的结果:

【讨论】:

  • 是的,我已经看到了。不过我正在使用 NSStrings。
猜你喜欢
  • 2015-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多