如何为UIButton
和UILabel
轮换文字? 90度,180度。
答案 0 :(得分:167)
[*yourlabelname* setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
旋转图像
透视图像
答案 1 :(得分:32)
试试这个:
lbl.transform= CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(270));
答案 2 :(得分:11)
我想提供另一种回应。
您可以通过从UILabel
派生子类并覆盖UILabel
,而不是旋转drawRect
,而是在标签内旋转文本。如果您正在使用Interface Builder,则可以在Identity Inspector的Custom Class属性中指定此子类而不是UILabel
。这将允许您使用XIB构建UI,而不是以编程方式创建标签。唯一需要注意的是Interface Builder中的文本将水平显示。但是,它将在应用程序本身中垂直呈现。
#import "RotatedLabel.h"
@implementation RotatedLabel
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextRotateCTM(context, -(M_PI/2));
UIFont* systemFont17 = [UIFont systemFontOfSize:17.0];
CGSize textSize = [self.text sizeWithFont:systemFont17];
CGFloat middle = (self.bounds.size.width - textSize.height) / 2;
[self.text drawAtPoint:CGPointMake(-self.bounds.size.height, middle) withFont:systemFont17];
CGContextRestoreGState(context);
}
@end
答案 3 :(得分:4)
你喜欢这个,
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 70)];
label.numberOfLines = 2;
label.text = @"text";
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.highlightedTextColor = [UIColor blackColor];
label.textAlignment = UITextAlignmentLeft;
label.font = [UIFont systemFontOfSize:12];
//rotate label in 45 degrees
label.transform = CGAffineTransformMakeRotation( M_PI/4 );
[self addSubview:label];
[label release];
答案 4 :(得分:3)
根据我的经验,UIView框架在应用变换后会发生变化,所以这就是我所使用的:
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(x, 0, 28, 159)];
l.textAlignment = NSTextAlignmentRight;
l.text = @"Hello!";
[_viewXAxisLabels addSubview:l];
[l setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
l.frame = CGRectMake(x, 0, 28, 159);
答案 5 :(得分:1)
//Right To Left
lable.transform = CGAffineTransformMakeRotation (3.14/2);
//Left To Right
[lable setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
OR
lable.transform= CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(270));
答案 6 :(得分:1)
lbl.transform=CGAffineTransformMakeRotation(M_PI);
//Go back
lbl.transform=CGAffineTransformMakeRotation(0);