简体   繁体   中英

UILabel extra spaces before and after text ios

Label shows text perfectly in portrait mode.
It also works properly when device rotates from portrait to landscape or landscape to portrait.

But when i open app in landscape mode it shows extra spaces before and after the text.

I have used NumberOfLines = 0 and sizeToFit .
and also tried

CGSize aSize = [self.label sizeThatFits:CGSizeMake(self.label.frame.size.width, CGFLOAT_MAX)];
self.label.frame = CGRectMake(self.label.frame.origin.x, self.label.frame.origin.y, self.label.frame.size.width, aSize.height);

You can check image here: http://picpaste.com/Screen_Shot_2014-02-14_at_4.44.42_PM-7I55uWNF.png

Add this code on your UIViewController

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [myLabel sizeToFit];
    [self.view layoutIfNeeded];

    myLabel.preferredMaxLayoutWidth = myLabel.frame.size.width;
    [self.view layoutIfNeeded];
}

使用情节提要中的自动调整大小

self.label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;

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