Dynamic Type within Labels of Basic (Static) Cells of UITableViewController - is it possible?

£可爱£侵袭症+ 提交于 2021-02-11 07:26:34

问题


Question is fairly self explanatory and I may be missing something simple. The following is done in Interface Builder.

I have created a UITableViewController, I've set the Content to Static Cells and Style to Grouped.

I've defined the number of Sections as well as the number of Rows for each section. I've set the Style of each cell to be Basic.

Now, within each cell, I have the default Content view and Label. For the label I have set the Text to Plain, Font to Body and Lines to 0. Normally this allows me to achieve Dynamic Type with UILabels.

I hook up the cells as IBOutlets and set their text (only) programmatically.

When I run the app, everything is there, except that the Font of the labels (as well as the height of each cell) is unchanged (not Dynamic Type).

Therefore, my question is, can Dynamic Type be achieved for a label within a Basic (non Custom) Static Cell or are these cells meant to be "as they are"?


The closest question I've seen on SO is this, however it refers to Custom Cells.

Also tried implementing

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.rowHeight = UITableViewAutomaticDimension
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

but to no avail.


回答1:


Dynamic type does work with static tables, but in my tests, just not with "Basic" cell type. Change it to a "Custom" cell type, add your label and the four constraints, and make sure to choose one of the dynamic fonts (e.g. "Title 3").

If you want the cell height to adjust accordingly, make sure you have both top and bottom constraints. And as you discovered, you can't just set the row height to UITableViewAutomaticDimension, but rather you have to implement that delegate method, e.g. in Swift 3:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.estimatedRowHeight = 44
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}



来源:https://stackoverflow.com/questions/40467556/dynamic-type-within-labels-of-basic-static-cells-of-uitableviewcontroller-is

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!