【发布时间】:2014-03-27 09:47:26
【问题描述】:
我在自定义 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); }
【问题讨论】:
-
这是你的初始化代码吗?你能显示你调整标签大小的部分吗?
-
您首先在哪里创建按钮和标签?因为您似乎是通过代码将它们添加到视图中?
-
我在界面生成器中创建了 uibutton 和 uilabel。我只在我的代码中更改了两者的配置
-
如果你想实现这一点,那么更好的选择是以编程方式创建并基于字符串值动态设置按钮和标签的框架。
-
你使用自动布局吗?
标签: ios objective-c uiview uibutton uilabel