I have a simple test app built with Xcode 4.5 and running on the Simulator for iOS6
It's just a single UIViewController with a UIScrollView and in the UIScrollView a single UILabel.
I set the NumberOfLines in the UILabel to 0 in the interface builder and made it the full width within the ScrollView - but only about 30 points in height. I also set the UIScrollView to vertical scrolling only. I set a couple of IBOutlets for the two UI components and set the file's owner as the delegate for the UIScrollView (and put on the UIViewController header.)
In the viewDidLoad I just set the text of the label to a nice long string.
- (void)viewDidLoad
{
[super viewDidLoad];
self.txtLabel2.text = @"`Twas brillig, and the slithy toves - Did gyre and gimble in the wabe: - All mimsy were the borogoves, - And the mome raths outgrabe. ";
}
and then in the ViewDidAppear I set the content size of the scroll view to the size of the UILabel:
-(void)viewDidAppear:(BOOL)animated
{
self.scrollView.contentSize = self.txtLabel2.bounds.size;
}
This seems to work well - the UILabel adjusts its height automagically and the scroll view is set to the correct height and scrolls nicely through the text. Great.
However, I'm obviously doing something serious wrong here - because if I go into the interface builder and change the Font size of the UILabel from the default (17.0) to any other value - it stops working. The label no longer resizes and is truncated at the first line.
I have tried manually resizing the UILabel - for example using the solution suggested here:
and using the extension when I set the content:
self.txtLabel2.text = @"`Twas brillig, and the slithy toves - Did gyre and gimble in the wabe: - All mimsy were the borogoves, - And the mome raths outgrabe. ";
//use the current width - resize the height
[self.txtLabel2 sizeToFitFixedWidth:self.txtLabel2.bounds.size.width];
but this doesn't seem to have any effect.
I don't understand why changing the font size should change the behaviour of the UILabel automatically resizing and I don't know why manually setting the size of the UILabel has no effect. What is happening here? (and also - what should I do differently so that I don't get into this mess in the first place!)