-4

我想为 NSString 使用 CGAffineTransformMakeRotation,我该怎么做?它使用标签,我的函数只需要字符串。

4

2 回答 2

3

CGAffineTransformMakeRotation用于设置 a 的transform属性UIViewNSString不是 aUIView所以不能变形,没有旋转的概念

您需要在显示时使用UILabel

一个例子:

NSString *myString = @"whatever";
CGAffineTransform stringTransform = CGAffineTransformMakeRotation(M_PI / 2.0f);

UILabel *stringLabel = [[UILabel alloc] initWithFrame:labelFrame];
stringLabel.transform = stringTransform;
stringLabel.text = myString;

[self.view addSubview:stringLabel];
于 2012-08-05T08:49:23.267 回答
0

您可以像下面的代码一样旋转文本标签:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib
    label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    label.text = @"fof fof fof";
    [self.view addSubview:label];
}

- (IBAction)rotate:(id)sender {
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(endAnimation)];
    CGAffineTransform rotate = CGAffineTransformMakeRotation(90.0f * (M_PI / 180.0f));
    label.transform = rotate;

    [UIView commitAnimations];  
}
于 2012-08-05T09:57:30.430 回答