Reputation: 3693
I want to know if there is a dynamic way of populating an UIView.
For example, Consider I have a couple of UILabels aligned as rows, whose text is again dynamic.
If the text for the first UILabel
has multiple lines, I know I can call
[myLabel sizeToFit];
to resize itself based on its content.
Is there an easy way of propagating this information to all the subviews (UILabels in my case) of the UIView and making them and respace them accordingly.
I hope I am clear enough.
Upvotes: 0
Views: 224
Reputation: 2543
For two labels that should be aligned as rows, you can use the CGRectGetMaxY
function. For example :
UILabel *first = [[UILabel alloc] initWithFrame:CGRectMake(10.0f,20.0f, 100.0f, 200.0f)];
[first setText:@"Text to display"];
[first sizeToFit];
[self.view addSubview:first];
[first release];
UILabel *second = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, CGRectGetMaxY(first.frame), 100.0f, 200.0f];
[second setText:@"Text to display"];
[second sizeToFit];
[self.view addSubview:second];
[second release];
UILabel *third = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, CGRectGetMaxY(second.frame), 100.0f, 200.0f];
[third setText:@"Text to display"];
etc...
You can also know the expected size of a label according to its text parameter doing this :
NSString *text = @"Text to display";
CGSize size = [text sizeWithFont:[UIFont fontWithName:@"HelveticaNeue" size:10.0f] constrainedToSize:CGSizeMake(100.0f, FLT_MAX) lineBreakMode:UILineBreakModeTailTruncation];
UILabel *first = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 100.0f, size.height)];
[first setText:text];
Upvotes: 1