简体   繁体   中英

Manually rotate and resize UILabel on orientation change

In my previous question I shared my problem with the black background appearing in my app on orientation change: Black background when view rotates on orientation change

I did not manage to solve the problem by following any of the advices I got and I have the feeling that the only way I can avoid the black background is by manually rotating my subviews on orientation change?

One of my subviews is a UILabel which is supposed to cover the entire screen. The rotation is going pretty well using a line of code similar to this one:

myLabel.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(isLandscape ? 90 : 0));

My problem is to make the UILabel adjust to take up the entire screen in landscape mode as well. I have tried to switch height and width of its bounds, but nothing happens.

Any suggestions will be greatly appreciated.


Here are some more code details:

[UIView animateWithDuration:0.5
                 animations:^{
                     myLabel.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(isLandscape ? 90 : 0));
                 }
                 completion:^(BOOL finished) {
                     myLabel.bounds = CGRectMake(0, 0, [self screenWidth], [self screenHeight]);

                     CGFloat fontSize = ((isLandscape ? 0.9649 : 0.9375) * [self screenWidth]) / 2.74;
                     [myLabel setFont:[UIFont fontWithName:FontName size:fontSize]];
                 }
];

where

- (CGFloat) screenWidth {
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    return isLandscape ? MAX(screenSize.width, screenSize.height) : MIN(screenSize.width, screenSize.height);
}

- (CGFloat) screenHeight {
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    return isLandscape ? MIN(screenSize.width, screenSize.height) : MAX(screenSize.width, screenSize.height);
}

Perhaps you are calling sizeToFit on the label somewhere?

Try instead to set:

myLabel.adjustsFontSizeToFitWidth  = YES;
myLabel.minimumFontScale      =  0.1; 

This will adjust your labels font size to fit the width of label.

我发现,一旦取消选中自动布局复选框,便能够调整标签尺寸。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM