How can you rotate text for UIButton
and UILabel
? 90 degrees, 180 degrees.
Asked
Active
Viewed 4.6k times
70
-
u just want to rotate the text or entire label/button? – NIKHIL Jun 13 '11 at 04:43
-
1[Swift version of this question](http://stackoverflow.com/questions/28717634/swift-how-can-you-rotate-text-for-uibutton-and-uilabel) – Suragch Jun 10 '15 at 03:07
7 Answers
168
[*yourlabelname* setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
rotated image
pervious image

Gypsa
- 11,230
- 6
- 44
- 82
-
7just a minor comment; along with `M_PI` you have access to `M_PI_2` (`pi / 2`) and `M_PI_4` (`pi / 4`) so you can use those. – Gabi Purcaru Oct 06 '13 at 16:23
33
Try this:
lbl.transform= CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(270));

DivineDesert
- 6,924
- 1
- 29
- 61
11
I wanted to provide an alternative response.
Instead of rotating the UILabel
, you can rotate the text within the label by deriving a subclass from UILabel
and overriding drawRect
. If you're using Interface Builder, you can specify this subclass instead of UILabel
in the Custom Class attribute of the Identity Inspector. This will allow you to build out your UI with XIBs, instead of programmatically creating the labels. The only caveat being that the text in Interface Builder will display horizontally. However, it will be rendered vertically in the app itself.
#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
-
Attempting to rotate the label inside a button will cause the label to be very short, for example, if the button is only as "wide" as the "height" of the label (when it is rotated). Applying the transform as given in the other answers to the UIButton rather than the button's label will rotate both the button and the label. – Matt Dec 04 '13 at 02:32
4
You do like this,
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];

EXC_BAD_ACCESS
- 2,699
- 2
- 21
- 25
-
-
Maybe its make sense to combine...but maybe any ideas how to implement it? – Oleg Aug 06 '12 at 06:07
-
2
3
In my experience, the UIView frame is changed after applying the transform, so this is what I've used:
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);

Roozbeh Zabihollahi
- 7,207
- 45
- 39
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));

Bhavesh Nayi
- 3,626
- 1
- 27
- 42
1
lbl.transform=CGAffineTransformMakeRotation(M_PI);
//Go back
lbl.transform=CGAffineTransformMakeRotation(0);

Bhavesh Nayi
- 3,626
- 1
- 27
- 42

Denis
- 21
- 1