2

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

  • 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); –  Feb 14 '14 at 11:07
  • I believe this question has been answered here already: http://stackoverflow.com/questions/3476646/uilabel-text-margin – ff10 Feb 14 '14 at 11:24
  • post image of simulator.. – NiravPatel Feb 14 '14 at 11:26
  • I have not enough reputation to upload image. –  Feb 14 '14 at 11:30

2 Answers2

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];
}
Free User
  • 211
  • 2
  • 14
0

Use Autosizing from storyboard OR

self.label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
iAhmed
  • 6,556
  • 2
  • 25
  • 31
  • if u cannot post a screenshot here then upload it on dropbox and paste a link here. By looking at it may be i could be able to help you :) – iAhmed Feb 14 '14 at 11:33