我有一个视图,其中有很多标签被填充了解析的XML。该视图位于UIScrollView中。一些XML对于标签来说太长了。我正在根据XML内容调整标签的大小。当标签重新调整大小时,它们会重叠后续标签,因为我的UIView(即UIScrollView内)没有重新调整大小。我一直在搜寻互联网几个小时,试图找到答案,但我试过的一切都没有达到我所期望的。任何帮助将不胜感激。根据内容调整UIView的大小
这是我的populateLabels方法。
-(void) populateLabels {
NSString *noInfo = (@"No Information Available");
lblCgName.text = _Campground.campground;
NSString *cgLoc = _Campground.street1;
NSString *cgCity = _Campground.city;
NSString *cgState = _Campground.state1;
NSString *cgCountry = _Campground.country;
NSString *cgZipPostal = _Campground.zipPostal;
cgLoc = [[cgLoc stringByAppendingString:@", "] stringByAppendingString:cgCity];
cgLoc = [[cgLoc stringByAppendingString:@", "] stringByAppendingString:cgState];
cgLoc = [[cgLoc stringByAppendingString:@", "] stringByAppendingString:cgCountry];
cgLoc = [[cgLoc stringByAppendingString:@" "] stringByAppendingString:cgZipPostal];
lblCgLoc.text = cgLoc;
double dRate = [_Campground.regRate1 doubleValue];
double dRate2 = [_Campground.regRate2 doubleValue];
NSString *rate = [[NSString alloc] initWithFormat:@"$%0.2f",dRate];
NSString *rate2 = [[NSString alloc] initWithFormat:@"$%0.2f",dRate2];
if ([rate2 isEqualToString:@"$0.00"]) {
lblRate.text = rate;
} else {
rate = [[rate stringByAppendingString:@" - "] stringByAppendingString:rate2];
lblRate.text = rate;
}
double dPaRate = [_Campground.paRate1 doubleValue];
double dPaRate2 = [_Campground.paRate2 doubleValue];
NSString *paRate = [[NSString alloc] initWithFormat:@"$%0.2f",dPaRate];
NSString *paRate2 = [[NSString alloc] initWithFormat:@"$%0.2f",dPaRate2];
if ([paRate2 isEqualToString:@"$0.00"]) {
lblPaRate.text = paRate;
} else {
paRate = [[paRate stringByAppendingString:@" - "] stringByAppendingString:paRate2];
lblPaRate.text = paRate;
}
lblLocal1.text = _Campground.localPhone1;
lblLocal2.text = _Campground.localPhone2;
lblTollFree.text = _Campground.tollFree;
lblFax.text = _Campground.fax;
lblEmail.text = _Campground.email;
lblWebsite.text = _Campground.website;
NSString *gps = _Campground.latitude;
NSString *longitude = _Campground.longitude;
gps = [[gps stringByAppendingString:@", "] stringByAppendingString:longitude];
lblGps.text = gps;
lblHighlights.text = _Campground.highlights;
lblHighlights.lineBreakMode = UILineBreakModeWordWrap;
lblHighlights.numberOfLines = 0;
//label resizing
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [_Campground.highlights sizeWithFont:lblHighlights.font
constrainedToSize:maximumLabelSize
lineBreakMode:lblHighlights.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = lblHighlights.frame;
newFrame.size.height = expectedLabelSize.height;
lblHighlights.frame = newFrame;
lblTents.text = _Campground.tents;
lblNotes.text = _Campground.notes;
lblDirections.text = _Campground.directions;
lblRentals.text = _Campground.rentals;
}
你可以参考http://stackoverflow.com/questions/2998336/how-to-create-a-uiscrollview-programmatically,这将帮助你创建你想要的那种视图... – rishi 2012-01-13 17:08:52
我欣赏输入,但那不是我正在寻找的。他们使用代码创建滚动视图并调整其大小。我正在重新调整我已经拥有的滚动视图中的视图大小。一切正常,它会一路向下滚动。我只需要扩展标签所在的视图。 – tallybear 2012-01-13 17:39:38
我可以建议你首先创建所有的标签,现在你可以计算出视图高度的标签 - label_height + offset_height_between_label + header_footer_offset。现在有了这个高度,你可以创建你的视图,然后你可以在那里添加标签。 – rishi 2012-01-13 19:52:54