UILabel extra spaces before and after text ios

為{幸葍}努か 提交于 2019-12-24 00:46:48

问题


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


回答1:


Add this code on your UIViewController

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

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



回答2:


Use Autosizing from storyboard OR

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


来源:https://stackoverflow.com/questions/21777620/uilabel-extra-spaces-before-and-after-text-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!