简体   繁体   English

如何自动调整单元格内容的字体大小以适合 tableview 宽度?

[英]How do I auto adjust the font size of a cells content to fit the tableview width?

I have a table view that shows the user what trips they have logged in. Except if the trip name is too long and doesn't fit within the table views width, it doesn't fully show.我有一个表格视图,向用户显示他们登录的行程。除非行程名称太长并且不适合表格视图的宽度,否则它不会完全显示。 I would hope it's possible to simply resize the font so that it can fit within the table view, similar to the "Adjust to Fit" option that text fields have.我希望可以简单地调整字体大小,使其适合表格视图,类似于文本字段具有的“调整以适合”选项。 Thanks!谢谢!

As long as your label is properly constrained within the tableViewCell, you can simply set:只要您的标签在 tableViewCell 中被正确约束,您就可以简单地设置:

label.adjustsFontSizeToFitWidth = true

Or if you're using the default UITableViewCell's textLabel, then in cellForRowAtIndexPath:或者,如果您使用默认的 UITableViewCell 的 textLabel,则在 cellForRowAtIndexPath 中:

cell.textLabel?.adjustsFontSizeToFitWidth = true

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

相关问题 如何获取tableview标头以调整字体以适合宽度 - How to get tableview header to adjust font to fit width 如何在UIButton中调整文本的字体大小以在Swift中以编程方式适应宽度? - How to adjust font size of text in a UIButton to fit the width programmatically in Swift? 如何在Swift 3中调整字体大小以适合高度(而非宽度) - How to adjust font size to fit height (not width) in swift 3 如何调整字体大小以适应UILabel的高度和宽度 - How to adjust font size to fit height and width of UILabel 如何自动调整 UIScrollView 的大小以适合其内容 - How do I auto size a UIScrollView to fit its content Swift 3 - 调整字体大小以适应宽度、多行 - Swift 3 - Adjust Font Size to Fit Width, Multiple Lines Swift:我如何将单元格放在一起并根据文本的数量调整TableView的单元格宽度 - Swift: How can i put cells together and adjust the cell width of a TableView depending on the amount of text 将 UIButton 字体大小调整为宽度 - Adjust UIButton font size to width 如何调整UITextView的宽度以适应其内容而不包装? - How to adjust UITextView's width to fit its content without wrapping? 如何根据段宽调整UISegmentedControl字体大小? - How to adjust UISegmentedControl font size based on segment width?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM