根据内容调整UIView的大小(Resizing UIView based on content)

编程入门 行业动态 更新时间:2024-10-26 04:24:29
根据内容调整UIView的大小(Resizing UIView based on content)

我有一个视图,其中许多标签都填充了解析的XML。 该视图位于UIScrollView中。 某些XML对于标签来说太长了。 我正在根据XML内容调整标签大小。 当标签重新调整大小时,它们会与后续标签重叠,因为我的UIView(在UIScrollView中)没有重新调整大小。 我一直在网上搜索数小时试图找到答案,但我尝试过的一切都没有达到我想要的水平。 任何帮助将不胜感激。

这是我的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; }

I have a view with a lot of labels being populated with parsed XML. That view is within a UIScrollView. Some of the XML is too long for the labels. I'm resizing the labels based on the XML content. When the labels are re sized, they overlap the subsequent labels because my UIView (that is within the UIScrollView) isn't being re sized. I've been scouring the internet for hours trying to find an answer to this, but everything that I've tried hasn't achieved what I'm looking for. Any help would be greatly appreciated.

This is my populateLabels method.

-(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; }

最满意答案

- (void) populateLabels方法位于何处? 如果它位于视图控制器中,您应该可以轻松访问需要调整大小的视图。 或者,如果标签(看起来已经创建)已添加到需要调整大小的视图中,则可以访问其超级视图。 您已经获得了所需的大小,只需在方法结束时设置superview的帧大小。

如果您尝试首先根据字符串内容查找每个标签的大小,则需要使用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;

如果您还不知道它,它将为您提供超视图的高度(如果需要,您可以对宽度执行相同的操作)。

Where is the - (void) populateLabels method located? If it's in a view controller, you should have easy access to the view that needs to be resized. Alternatively, if the labels (which look to already be created) have been added to the view that needs to be resized, then you can access their superview. You've already got size you need, just set set the superview's frame size at the end of the method.

If you're trying to find the size of each label first based on the string content, you'll want to use the NSString method: -sizeWithFont. There are several variations of this that allow you to specify constraints:

Computing Metrics for a Single Line of Text – sizeWithFont: – sizeWithFont:forWidth:lineBreakMode: – sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: Computing Metrics for Multiple Lines of Text – sizeWithFont:constrainedToSize: – sizeWithFont:constrainedToSize:lineBreakMode:

Remember that UILables only show 1 line of text. Also keep in mind that this will return the size that the string will take up. Views like UILabels and UITextViews tend to add padding, so you may need to account for that when calculating the height. After you've calculated the height and placed each label, you should then know the size the superView will need to be and set it appropriately. Also, if you don't know which label will be last/lowest/furthest down in the view (the size of the superView should be the origin.y + the size.height of the last view) you can do something like this:

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;

This will give you the height of the superview if you don't already know it (and you can do the same thing for width if need be).

更多推荐

本文发布于:2023-07-31 22:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1348599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小   内容   UIView   content   Resizing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!