ios 如何在 Objective-C 中为 UIButton 和 UILabel 旋转文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6326720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:19:34  来源:igfitidea点击:

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

iosobjective-cuibuttonrotationuilabel

提问by jdl

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

你如何为UIButton和旋转文本UILabel?90 度,180 度。

回答by Gypsa

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

rotated image enter image description here

旋转图像 在此处输入图片说明

pervious image enter image description here

上一张图片 在此处输入图片说明

回答by DivineDesert

Try this:

尝试这个:

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

回答by Andy S

I wanted to provide an alternative response.

我想提供另一种回应。

Instead of rotating the UILabel, you can rotate the text withinthe label by deriving a subclass from UILabeland overriding drawRect. If you're using Interface Builder, you can specify this subclass instead of UILabelin 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.

UILabel您可以通过从 派生和覆盖的子类来旋转标签的文本,而不是旋转。如果您使用的是 Interface Builder,则可以指定此子类,而不是在 Identity Inspector 的 Custom Class 属性中。这将允许您使用 XIB 构建您的 UI,而不是以编程方式创建标签。唯一需要注意的是,Interface Builder 中的文本将水平显示。但是,它将在应用程序本身中垂直呈现。UILabeldrawRectUILabel

#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

回答by EXC_BAD_ACCESS

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

回答by Roozbeh Zabihollahi

In my experience, the UIView frame is changed after applying the transform, so this is what I've used:

根据我的经验,应用变换后 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);

回答by Bhavesh Nayi

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

回答by Denis

lbl.transform=CGAffineTransformMakeRotation(M_PI);

//Go back 

lbl.transform=CGAffineTransformMakeRotation(0);