About trouble of device rotation using MTLabel

2019-08-23 11:21发布

问题:

I have to use MTLabel Class for line-spacing of UILabel.

( refered sample code : https://github.com/Tuszy/MTLabel )

But Some problems exists.

I'm making a iPad App. This app is able to rotate - landscape or portrait.

I put UILabel and MTLable objects on view without IB.

Whenever orientation of device is changed, width of text also changed.

This result is not what I want.

My code :

#import "MTLabel.h"

.

- (void)viewDidLoad
{
    [super viewDidLoad];

     MTLabel *TitleFont = [[MTLabel alloc] initWithFrame:CGRectMake(255, 60, 270, 60)];

    [TitleFont setFont:[UIFont fontWithName:@"Arial" size:30.0]];

    TitleFont.backgroundColor = [UIColor greenColor];
    TitleFont.text = @"Happy! - 1";
    TitleFont.autoresizingMask = UIViewAutoresizingFlexibleTopMargin& UIViewAutoresizingFlexibleLeftMargin&UIViewAutoresizingFlexibleRightMargin;

    TitleFont.autoresizingMask= UIViewAutoresizingFlexibleWidth;

    [self.view addSubview:TitleFont];

    //----------------------

    UILabel *TitleFont2 = [[UILabel alloc] initWithFrame:CGRectMake(255, 120, 270, 60)];

    [TitleFont2 setFont:[UIFont fontWithName:@"Arial" size:30.0]];

    TitleFont2.backgroundColor = [UIColor orangeColor];
    TitleFont2.text = @"Happy! - 2";
    TitleFont2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin& UIViewAutoresizingFlexibleLeftMargin&UIViewAutoresizingFlexibleRightMargin;

    TitleFont2.autoresizingMask= UIViewAutoresizingFlexibleWidth;

    [self.view addSubview:TitleFont2];



}

Result : 1) portrait image :

2) landscape image :

If I use UILabel class, no problem! But I have to MTLabel class for line-spacing of UILabel.

Please help me... Thank you.

回答1:

try this:

[TitleFont setContentMode:UIViewContentModeRedraw];


回答2:

You'll need to kick MTLabel with setNeedsDisplay to redraw itself properly in the new size. Probably in didRotateFromOrientation.



回答3:

[TitleFont setNeedsDisplay]

try this ,it works for me. and sure you should disable setAutoresizesSubviews:<NO>