将标签添加到滚动视图

时间:2012-08-18 20:19:01

标签: iphone ios scrollview

我正在尝试向滚动视图添加标签,然后设置scrollview的contentSize以匹配所需的标签大小。问题是我似乎必须使contentSize大于标签的大小:

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;

这里我认为我得到了必要的标签大小来包含文本

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;

这里我将contentSize设置为标签的大小,但它不够大,我需要 将它设置为bodyLabel.frame.size.height + 80,以便在滚动中覆盖它。

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

提前感谢您的任何想法!

1 个答案:

答案 0 :(得分:1)

[theScrollview addSubView: bodyLabel];