UILabel has a maximum height?

Not too much code to share, just a question. Why is my label invisible if its height exceeds 8191 pixels?

You might think that this is too much, and why in the world I want such a long label ... it is dynamic, so you never know.

So, this is how it goes: I create my UIScrollView and start adding labels to init, of which 5. I set the text and went well. I have a method that will take 5 labels, get the size using NSString sizeWithFont:constrainedToSize:lineBreakMode: change them and reset the contentHeight UIScrollView. Things are good. The fact is that when the mark exactly exceeds 8191 pixels in height (and 300 in width), it disappears, is not visible, is tightened! gone.

If I cannot get this to work, I think I can split the text into several parts and create additional UILabels, but I want to avoid this.

Any ideas?

Here is some dummy code, easy to follow

 NSError *er = nil; // this file can be found here: // https://gist.github.com/3167635 NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"lorem.txt"]; NSString *labelText = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&er]; if(er != nil) NSLog(@"Error: %@", er); UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: self.view.bounds]; [scrollView setBackgroundColor:[UIColor whiteColor]]; UILabel *label = [[UILabel alloc] init]; [label setBackgroundColor:[UIColor whiteColor]]; [label setNumberOfLines:0]; [label setText:labelText]; CGSize size = [labelText sizeWithFont:label.font constrainedToSize:CGSizeMake(300, CGFLOAT_MAX) lineBreakMode:UILineBreakModeCharacterWrap]; NSLog(@"Size: %@", NSStringFromCGSize(size)); CGRect labelFrame; labelFrame.origin = CGPointMake(10, 0); labelFrame.size = size; [label setFrame:labelFrame]; [scrollView setContentSize:size]; [scrollView addSubview:label]; [[self view] addSubview:scrollView]; 

The fictitious text is huge, making the label invisible.

+4
source share
1 answer

I suggest using the sizeToFit property instead of setting the height yourself, since this property will set the height of the label according to the ur text, you do not need to make efforts to adjust the height

or you can use this line instead

 CGSize maximumLabelSize = CGSizeMake(headerView.frame.size.height,over 8191 ); 
0
source

All Articles