2012-01-13 168 views
3

我有一个视图,其中有很多标签被填充了解析的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; 

} 
+0

你可以参考http://stackoverflow.com/questions/2998336/how-to-create-a-uiscrollview-programmatically,这将帮助你创建你想要的那种视图... – rishi 2012-01-13 17:08:52

+0

我欣赏输入,但那不是我正在寻找的。他们使用代码创建滚动视图并调整其大小。我正在重新调整我已经拥有的滚动视图中的视图大小。一切正常,它会一路向下滚动。我只需要扩展标签所在的视图。 – tallybear 2012-01-13 17:39:38

+0

我可以建议你首先创建所有的标签,现在你可以计算出视图高度的标签 - label_height + offset_height_between_label + header_footer_offset。现在有了这个高度,你可以创建你的视图,然后你可以在那里添加标签。 – rishi 2012-01-13 19:52:54

回答

3

- (void) populateLabels方法在哪里?如果它在视图控制器中,则应该可以轻松访问需要调整大小的视图。或者,如果标签(看起来已经创建)已被添加到需要调整大小的视图中,则可以访问其超级视图。你已经有了你需要的尺寸,只需在方法的结尾处​​设置超级视图的帧尺寸即可。

如果您试图首先根据字符串内容查找每个标签的大小,则需要使用NSString方法:-sizeWithFont。有迹象表明,允许您指定的约束这几个变化:

计算度量的文本 单行 - sizeWithFont: - sizeWithFont:forWidth:lineBreakMode: - sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: 计算度量的文本 的多行 - sizeWithFont:constrainedToSize: - sizeWithFont:constrainedToSize:lineBreakMode:

记住UILables只显示1行文字。另外请记住,这将返回字符串将占用的大小。像UILabels和UITextViews这样的视图倾向于添加填充,所以在计算高度时可能需要考虑这一点。在计算好高度并放置每个标签后,您应该知道superView需要的尺寸并进行适当的设置。另外,如果你不知道在视图中最后/最低/最下的标签(superView的大小应该是origin.y +最后一个视图的size.height),你可以这样做:

CGFloat totalHeight = 0.0f; 
for (UIView *view in superView.subviews) 
    if (totalHeight < view.frame.origin.y + view.frame.size.height) totalHeight = view.frame.origin.y + view.frame.size.height; 

如果你还不知道它的话,这会给你超级视图的高度(如果需要,你可以对宽度做同样的事情)。

+0

populateLabels方法位于我的CampgroundDetailsViewController.m文件中。在该文件的笔尖中,我将所有对象(标签,文本视图,按钮等)放在视图中。然后,该视图位于滚动视图内。有什么我做错了吗? – tallybear 2012-01-13 23:24:44

+0

不是我可以看到,在该方法结束时,只是调整包含标签/文本/视图/按钮/等的视图的大小,并且还要设置滚动视图的contentSize以适应。您应该可以通过CampgroundDetailsViewController的类实例变量访问这两个视图。 – 2012-01-13 23:45:09

+0

你知道如何根据内容动态设置视图吗?我听说我需要使用sizeToFit,但我应该通过什么? – tallybear 2012-01-16 16:10:55

1

你真的需要使用一堆标签吗?
看来你需要使用UITextView或UIScrollView。

+0

我曾考虑过使用UITextView,但问题在于我的目标人群。我正在构建的应用程序专为RVers设计。我们目前有一个iPhone应用程序,我们得到的投诉多于赞誉。它使用UITextView作为“Directions”,“Highlights”和“Notes”部分。他们抱怨说,一旦他们到达TextView,他们就无法滚动视图。我们的投诉中有99%是因为我们的会员不知道如何使用他们的设备。 – tallybear 2012-01-13 17:46:21

+0

如果UITextView中的内容少于UITextView本身,则它将不可滚动。我看不到在这里不使用UITextView的原因。但这取决于你。 – 2012-01-13 17:56:29

+0

我到了要告诉老人们处理它并使用UITextView的地步。我刚刚用TextView替换了“Highlights”标签,它的功能非常出色。那么我不必调整视图的大小。我可以使它静态,加载一堆TextViews,而不用担心。 – tallybear 2012-01-13 17:56:39

1

基于其子视图调整UIView大小的一般方法是sizeToFit 此方法自动调整您的UIView大小。

如果您想限制大小,您需要使用sizeThatFits:此方法不会自动调整您的UIView大小,而是返回大小。

只是侧面说明:这两种方法只影响子视图,所以不是图纸或核心文本。

+0

我试过在我的scrollView上使用sizeToFit,但它没有做任何事情。我在我的scrollView里面有一个View,一切都在View上。我应该以另一种方式去做吗? – tallybear 2012-01-13 23:23:25