我正在尝试将标签添加到滚动视图,然后设置滚动视图的 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 设置为标签的 Size,但是不够大,我需要将其设置为 bodyLabel.frame.size.height + 80 才能被滚动覆盖。
theScrollview.contentSize = CGSizeMake(bodyLabel.frame.size.width, bodyLabel.frame.size.height);
[theScrollview addSubview:bodyLabel];
感谢您提前提出任何想法!