How can you rotate text for UIButton and UILabel in Objective-C?

IosObjective CUibuttonRotationUilabel

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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

rotated image enter image description here

pervious image enter image description here

Solution 2 - Ios

Try this:

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

Solution 3 - Ios

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

Solution 4 - Ios

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

Solution 5 - Ios

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

Solution 6 - Ios

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

Solution 7 - Ios

lbl.transform=CGAffineTransformMakeRotation(M_PI);

//Go back 

lbl.transform=CGAffineTransformMakeRotation(0);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjdlView Question on Stackoverflow
Solution 1 - IosGypsaView Answer on Stackoverflow
Solution 2 - IosDivineDesertView Answer on Stackoverflow
Solution 3 - IosAndy SView Answer on Stackoverflow
Solution 4 - IosEXC_BAD_ACCESSView Answer on Stackoverflow
Solution 5 - IosRoozbeh ZabihollahiView Answer on Stackoverflow
Solution 6 - IosBhavesh NayiView Answer on Stackoverflow
Solution 7 - IosDenisView Answer on Stackoverflow