Iphone UIButton:根据标题标签文本计算UIButton的高度

Iphone UIButton:根据标题标签文本计算UIButton的高度,iphone,ios,uitableview,uibutton,uilabel,Iphone,Ios,Uitableview,Uibutton,Uilabel,我在自定义UITableViewCell中有一个自定义UIButton。我想根据按钮标题标签的可变文本内容设置自定义表格单元格中UIButton的高度 我尝试了下面的方法来计算高度 [NSString sizeWithFont:constrainedToSize:lineBreakMode:]; 但我无法根据标题标签成功更改按钮高度 此外,由于此按钮是自定义UITableViewCell的一部分,我还需要根据自定义UIButton的高度更改单元格的高度 在实现UITableView的委托和数据

我在自定义UITableViewCell中有一个自定义UIButton。我想根据按钮标题标签的可变文本内容设置自定义表格单元格中UIButton的高度

我尝试了下面的方法来计算高度

[NSString sizeWithFont:constrainedToSize:lineBreakMode:];
但我无法根据标题标签成功更改按钮高度

此外,由于此按钮是自定义UITableViewCell的一部分,我还需要根据自定义UIButton的高度更改单元格的高度

在实现UITableView的委托和数据源方法的ViewController中,我尝试用以下方法确定高度

- (CGFloat) tableView: (UITableView *) tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath
但是没有成功


解决这个问题的最佳解决方案或方法是什么?

看起来你做得不错,但请记住:

heightforrowatinexpath
必须返回
CGFloat
sizeWithFont
提供文本的高度,您需要添加按钮的填充和单元格

使用
constrainedToSize:CGSizeMake(#####,MAXFLOAT)
其中####是文本宽度


lineBreakMode:UILineBreakModeWordWrap
当然

看起来你做得不错,但请记住:

heightforrowatinexpath
必须返回
CGFloat
sizeWithFont
提供文本的高度,您需要添加按钮的填充和单元格

使用
constrainedToSize:CGSizeMake(#####,MAXFLOAT)
其中####是文本宽度


lineBreakMode:UILineBreakModeWordWrap
当然

假设按钮的宽度为145.0f:

CGSize constraintSize;

constraintSize.width = 145.0f;

constraintSize.height = MAXFLOAT;

CGSize size = [Label.text sizeWithFont:Label.font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 
然后可以设置帧:

frame = CGRectMake(0, 0, 145, size.height);

假设按钮的宽度为145.0f:

CGSize constraintSize;

constraintSize.width = 145.0f;

constraintSize.height = MAXFLOAT;

CGSize size = [Label.text sizeWithFont:Label.font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 
然后可以设置帧:

frame = CGRectMake(0, 0, 145, size.height);

您也可以使用
[buttonInstance sizeToFit]

 UIButton *lineButton = [[UIButton alloc] initWithFrame:CGRectZero];
 // If padding equals "yes, please"
 [lineButton setContentEdgeInsets:UIEdgeInsetsMake(0.0f, 5.0f, 0.0f, 3.0f)];
 [lineButton setTitle:@"Title" forState:UIControlStateNormal];
 [lineButton sizeToFit];

您也可以使用
[buttonInstance sizeToFit]

 UIButton *lineButton = [[UIButton alloc] initWithFrame:CGRectZero];
 // If padding equals "yes, please"
 [lineButton setContentEdgeInsets:UIEdgeInsetsMake(0.0f, 5.0f, 0.0f, 3.0f)];
 [lineButton setTitle:@"Title" forState:UIControlStateNormal];
 [lineButton sizeToFit];
在这里看到我的答案-在这里看到我的答案-