gpt4 book ai didi

iphone - 根据内容调整 UIView 的大小

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:24:02 26 4
gpt4 key购买 nike

我有一个 View ,其中有很多标签被解析的 XML 填充。该 View 位于 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;

}

最佳答案

- (void) populateLabels 方法在哪里?如果它在 View Controller 中,您应该可以轻松访问需要调整大小的 View 。或者,如果标签(看起来已经创建)已添加到需要调整大小的 View 中,那么您可以访问它们的 super View 。您已经获得了所需的尺寸,只需在方法末尾设置 super View 的帧尺寸即可。

如果您试图首先根据字符串内容找到每个标签的大小,您将需要使用 NSString 方法:-sizeWithFont。有几种允许您指定约束的变体:

计算单行文本的指标– 字体大小:– sizeWithFont:forWidth:lineBreakMode:– sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:多行文本的计算指标– sizeWithFont:constrainedToSize:– sizeWithFont:constrainedToSize:lineBreakMode:

请记住,UILables 仅显示 1 行文本。还要记住,这将返回字符串将占用的大小。 UILabels 和 UITextViews 之类的 View 往往会添加填充,因此您可能需要在计算高度时考虑到这一点。在计算出高度并放置每个标签后,您应该知道 superView 需要的大小并适本地设置它。此外,如果您不知道哪个标签将在 View 中位于最后/最低/最下方(superView 的大小应为最后一个 View 的 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;

如果您还不知道父 View 的高度,这将为您提供它(如果需要,您可以对宽度做同样的事情)。

关于iphone - 根据内容调整 UIView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8854277/

26 4 0
文章推荐: ios - 有没有办法仅在每个文件的基础上使用 ARC 迁移工具?
文章推荐: google-chrome - Chrome 不缓存 请求