By jdl


2011-06-13 04:38:42 8 Comments

How can you rotate text for UIButton and UILabel? 90 degrees, 180 degrees.

7 comments

@Andy S 2013-07-30 15:07:25

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

@Matt 2013-12-04 02:32:09

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.

@Bhavesh Nayi 2013-01-11 09:14:27

//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));

@Roozbeh Zabihollahi 2014-04-14 02:15:11

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);

@Denis 2013-01-31 19:36:13

lbl.transform=CGAffineTransformMakeRotation(M_PI);

//Go back 

lbl.transform=CGAffineTransformMakeRotation(0);

@DivineDesert 2011-06-13 04:43:37

Try this:

lbl.transform= CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(270));

@3lvis 2012-10-25 12:23:04

#define DEGREES_TO_RADIANS(x) (M_PI * x / 180.0)

@Denis 2013-09-19 16:05:40

CGAffineTransformMakeRotation(- M_PI_2);

@EXC_BAD_ACCESS 2011-06-13 09:09:15

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];

@Oleg 2012-08-05 08:41:44

Can i use the CGAffineTransformMakeRotation for NSString?

@Oleg 2012-08-06 06:07:54

Maybe its make sense to combine...but maybe any ideas how to implement it?

@mskw 2012-10-03 17:57:08

for some strange reason when i use M_PI/4 the label disappears.

@Gypsa 2011-06-13 04:47:12

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

rotated image enter image description here

pervious image enter image description here

@Gabi Purcaru 2013-10-06 16:23:04

just 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.

@Suragch 2015-02-26 04:41:12

Related Questions

Sponsored Content

34 Answered Questions

[SOLVED] How do I create a basic UIButton programmatically?

31 Answered Questions

[SOLVED] Activity restart on rotation Android

18 Answered Questions

26 Answered Questions

[SOLVED] How do you add multi-line text to a UIButton?

26 Answered Questions

[SOLVED] How do I sort an NSMutableArray with custom objects in it?

91 Answered Questions

22 Answered Questions

[SOLVED] How do I check if a string contains another string in Objective-C?

48 Answered Questions

[SOLVED] Vertically align text to top within a UILabel

5 Answered Questions

[SOLVED] How can you rotate text for UIButton and UILabel in Swift?

51 Answered Questions

[SOLVED] How to change Status Bar text color in iOS

Sponsored Content