Uba Duba
Uba Duba

Reputation: 241

Adding a label to a scroll view

I am trying to add a label to a scroll view, then set the contentSize of the scrollview to match the desired size of the label. The problem is I seem to have to make the contentSize bigger than the size of the label:

theScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,480)];
bodyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0, 50.0)];
bodyLabel.textColor = [UIColor whiteColor];
bodyLabel.backgroundColor = [UIColor clearColor];
bodyLabel.textAlignment = UITextAlignmentLeft;
bodyLabel.text = event.description; <- basically some long ass text here
bodyLabel.font=[UIFont systemFontOfSize:16.0];

bodyLabel.numberOfLines = 0;
bodyLabel.lineBreakMode = UILineBreakModeWordWrap;

here I think Im getting the necessary label size to contain the text

CGSize size = [event.description sizeWithFont:bodyLabel.font constrainedToSize:CGSizeMake(bodyLabel.frame.size.width, 9500) lineBreakMode:bodyLabel.lineBreakMode];

frame = bodyLabel.frame; // to get the width
frame.size.height = size.height;    
bodyLabel.frame = frame;

here I set the contentSize to the label's Size, but it isn't big enough, I need to set it to bodyLabel.frame.size.height + 80 for it to be covered in the scrolling.

theScrollview.contentSize = CGSizeMake(bodyLabel.frame.size.width, bodyLabel.frame.size.height);
[theScrollview addSubview:bodyLabel];

Thanks for any thoughts in advance!

Upvotes: 0

Views: 211

Answers (1)

Coder404
Coder404

Reputation: 742

[theScrollview addSubView: bodyLabel];

Upvotes: 1

Related Questions