gpt4 book ai didi

ios - 当 UILabel 动态调整大小时,移动 UIView 中的 UIButton。 iOS系统

转载 作者:行者123 更新时间:2023-11-29 03:06:45 24 4
gpt4 key购买 nike

我在自定义 UIView 中有一个 UILabel 和一个 UIButton。我的 UIButton 网站位于我的 UILabel 下方。

我的 UILabel 文本改变了它的大小并与我的 UIButton 重叠。

我希望能够在 UILabel 自行调整大小时向下移动 UIButton?

我只设法动态调整了我的 UILabel 的大小,但无法调整或移动我的 UIButton

self.chosenCategoriesLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:17.0];
self.chosenCategoriesLabel.textColor = [UIColor colorWithRed:161.0/255.0 green:205.0/255.0 blue:86.0/255.0 alpha:1.0];
self.chosenCategoriesLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:15.0];
self.chosenCategoriesLabel.numberOfLines = 0;
self.selectedCategoriesString = [self.selectedCategoriesArray componentsJoinedByString:@","];
self.chosenCategoriesLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.chosenCategoriesLabel.text = [self.selectedCategoriesString stringByAppendingString:@"\n\n\n\n"];
[self.selectionsWrapper addSubview:self.chosenCategoriesLabel];

self.chosenCategoriesLabel.frame = CGRectMake(self.chosenCategoriesLabel.frame.origin.x, self.chosenCategoriesLabel.frame.origin.y, self.chosenCategoriesLabel.frame.size.width, [self sizeLabel]);

[self.itemThumbnail setImageWithURL:self.item.itemImageURL placeholderImage:[UIImage imageNamed:@"profile-image-placeholder"]];
[self.view addSubview:self.itemThumbnail];

[self.selectCategorieButton setTitle:@"Select Categories" forState:UIControlStateNormal];
[self.selectCategorieButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.selectCategorieButton setTitle:@"Select Categories" forState:UIControlStateHighlighted];
[self.selectCategorieButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
self.selectCategorieButton.titleLabel.font = lightHelveticaFont;
[self.selectCategorieButton setBackgroundImage:[UIImage imageNamed:@"btn_choose_categories.png"] forState:UIControlStateNormal];
[self.selectCategorieButton setBackgroundImage:[UIImage imageNamed:@"btn_choose_categories.png"] forState:UIControlStateHighlighted];
[self.selectCategorieButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
[self.selectCategorieButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:15.0]];
[self.selectionsWrapper addSubview:self.selectCategorieButton];

[self changeLabel];

self.chosenCategoriesLabel.frame = CGRectMake(self.chosenCategoriesLabel.frame.origin.x, self.chosenCategoriesLabel.frame.origin.y, self.chosenCategoriesLabel.frame.size.width, [self sizeLabel]);

-(void)changeLabel {

self.chosenCategoriesLabel = [NSString stringWithFormat:@"%@",array];

self.chosenCategoriesLabel.frame = CGRectMake(self.chosenCategoriesLabel.frame.origin.x, self.chosenCategoriesLabel.frame.origin.y, self.chosenCategoriesLabel.frame.size.width, [self sizeLabel]);

}

- (CGFloat) sizeLabel
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
CGSize sizeToFit = [self.chosenCategoriesLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(276.0f, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
#pragma clang diagnostic pop
return fmaxf(22.0f, sizeToFit.height + 40); }

最佳答案

一些事情...

当您说您已经在 IB 中创建了 UI 组件时,我不确定您为什么要再次重新添加它们?例如,如果控件已经在 View 中,则不需要像下面这样的行

[self.selectionsWrapper addSubview:self.chosenCategoriesLabel];

这一行你也执行了两次?

self.chosenCategoriesLabel.frame = CGRectMake(self.chosenCategoriesLabel.frame.origin.x, self.chosenCategoriesLabel.frame.origin.y, self.chosenCategoriesLabel.frame.size.width, [self sizeLabel]);

要调整按钮的框架,请执行以下操作...

// Get the original frames;
CGRect buttonFrame = self.selectCategorieButton.frame;
CGRect labelFrame = self.chosenCategoriesLabel.frame;

//Work out the new height
CGFloat newHeight = [self sizeLabel];

//Work out what the difference is
CGFloat adjustment = newHeight - labelFrame.size.height;

//Set the labels new height
labelFrame.size.height = newHeight;
self.chosenCategoriesLabel.frame = labelFrame;

//add the difference to the buttons frame
buttonFrame.origin.y = buttonFrame.origin.y + adjustment;
self.selectCategorieButton.frame = buttonFrame;

根据您定义自定义 View 的方式,您可能还需要调整它的高度以适应按钮的新位置?

关于ios - 当 UILabel 动态调整大小时,移动 UIView 中的 UIButton。 iOS系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22683752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com