0

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:

Sizing a UILabel to fit?

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!)

Community
  • 1
  • 1
Pete McPhearson
  • 469
  • 1
  • 7
  • 17
  • This is a very simple little app - and I can start from scratch and get the same result. Is anyone able to reproduce this effect? – Pete McPhearson Oct 11 '12 at 12:15

1 Answers1

0

I dont know why your code is not working as there are multiple possibilities But foll code is good alternative for your code

int scrollViews_fixed_width = self.scrollView.bounds.size.width;
self.txtLabel2.lineBreakMode = UILineBreakModeWordWrap; 
self.txtLabel2.numberOfLines = 0;

UIFont *font = [UIFont fontWithName:@"Helvetica" size:14.0];//Choose Your Font this is default font
CGSize constraintSize = CGSizeMake(scrollViews_fixed_width, 555);// Choose big value max possible height of textLabel or leave it 555 this will be enough
float  calculatedHeight = [@"Your Long String" sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap].height;
self.txtLabel2.frame = CGRectMake(0,0,scrollViews_fixed_width,calculatedHeight);
self.scrollView.contentSize = CGSizeMake(scrollViews_fixed_width, calculatedHeight);

you may require to add some offset to height as above code calculation is not considering space bet lines if u r satisfied with above dont use foll implementation for this calculate height for single line using small string then use no_of_lines = calculatedHeight/ single_line_height_calculated_using_small_string and then put some additional offset to height depending on no. of lines

Pete McPhearson
  • 469
  • 1
  • 7
  • 17
Aditya Deshmane
  • 4,676
  • 2
  • 29
  • 35
  • This is another technique for setting the UILabel size and UIScrollView's content size - however it has the exact same behaviour as the method in the original question (i.e. it only seems to work with a font size of 17.0) – Pete McPhearson Oct 11 '12 at 12:10