问题
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