如何旋转UIButton
和的文字UILabel
?90度,180度
您只想旋转文本或整个标签/按钮?
—
NIKHIL 2011年
如何旋转UIButton
和的文字UILabel
?90度,180度
Answers:
尝试这个:
lbl.transform= CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(270));
我想提供另一种回应。
除了旋转UILabel
,还可以通过从中继承子类并重写来旋转标签内的文本。如果使用的是Interface Builder,则可以指定此子类,而不是在Identity Inspector的“自定义类”属性中指定。这将允许您使用XIB构建您的UI,而无需以编程方式创建标签。唯一的警告是Interface Builder中的文本将水平显示。但是,它将在应用程序本身中垂直呈现。UILabel
drawRect
UILabel
#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
你是这样的
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];
以我的经验,在应用转换后,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);
//Right To Left
lable.transform = CGAffineTransformMakeRotation (3.14/2);
//Left To Right
[lable setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
要么
lable.transform= CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(270));