简体   繁体   English

如何动态地使UITableView行与标签中的标签一样宽?

[英]How to make UITableView row as wide as the label in it dynamically?

I have a UITableView where each row may have a label anywhere from no lines to any number of lines (realistically 3-4). 我有一个UITableView,其中每行可能有一个标签,从无行到任意行(实际上是3-4)。 I am wanting the row to be expanded based on the UILabelView contents. 我希望基于UILabelView内容来扩展行。

I have this so far: 到目前为止,我有:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat  height        = 60.0;

UIFont *font        = [UIFont fontWithName:@"Helvetica-BoldOblique" size:10.0];
NSString *text      = reminder.notes; //the text that will be in the UiLabelView
CGFloat notesHeight = [text sizeWithFont:font
                               constrainedToSize:CGSizeMake(250.0, 4000.0)
                                   lineBreakMode:UILineBreakModeTailTruncation].height;

height += notesHeight;

return height;

}

I still get screwy results. 我仍然得到错误的结果。 What can I do to accomplish my goal? 我该怎么做才能实现自己的目标?

Thank you 谢谢

you are using UILineBreakModeTailTruncation as lineBreakMode which will always result in a single line and sizeWithFont:constrainedToSize:lineBreakMode: will then return the needed size of that single line. 您将UILineBreakModeTailTruncation用作lineBreakMode,这将始终导致单行,并且sizeWithFont:constrainedToSize:lineBreakMode:然后将返回该单行的所需大小。

Try UILineBreakModeWordWrap instead. 尝试改用UILineBreakModeWordWrap

edit: I hope you have set the numberOfLines -property of a UILabel to 0 (zero) so that your label is able to draw many lines. 编辑:希望您已将UILabelnumberOfLines -property设置为0 (零),以便您的标签能够绘制多条线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM