UILabel 的字体在视图重新出现之前不会调整 [英] UILabel's font not adjusting until view reappears

查看:36
本文介绍了UILabel 的字体在视图重新出现之前不会调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用故事板创建了一个带有标签的单元格的表格视图.

Using the storyboard I created a tableview with cells that have labels.

我希望能够轻松地为应用程序中的所有标签分配相同的字体,因此我在帮助文件(smallFont、mediumFont 和 largeFont)和 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath) 中创建了一些字体: NSIndexPath) 我将每个标签设置为使用我想要的字体(即 cell.label.font = Common.fonts.mediumFont).

I want to be able to easily assign the same font to all labels in my app so I created some fonts in a helper file (smallFont, mediumFont, and largeFont) and in tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) I set each label to use the font I want (i.e. cell.label.font = Common.fonts.mediumFont).

tableview 首次显示时使用的是故事板中定义的字体,并且在您离开视图并返回之前它不会使用我编程的字体.

The first time the tableview displays it is using the font defined in the storyboard and it doesn't use the font I programed in until you navigate away from the view and back.

我可以通过更改情节提要以使用与我以编程方式放置的字体相同的字体来消除视觉上的调整大小,但我希望能够仅通过调整通用文件中的字体来更改整个应用程序的字体.

I can eliminate the visual resizing by changing the storyboard to use the same font that I place programmatically but I want to be able to change the fonts across the entire app just by adjusting the font in the Common file.

当视图第一次出现时,如何覆盖故事板的字体并强制它使用编程的字体?

How can I override the storyboard's font and force it to use the programmed in font when the view first appears?

添加更多信息.

当我滚动表格时,标签字体会从故事板大小更改为编程字体.

When I scroll the table the label fonts change from the storyboard size to the programmed fonts.

推荐答案

我已经通过在 cellForRowAtIndexPathwillDisplayCell 方法中设置字体来修复它.

I have fixed it by setting the font in both cellForRowAtIndexPath and willDisplayCell methods.

willDisplayCell 是这样实现的:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, 
               forRowAt indexPath: IndexPath) {
    // e.g.
    let myCell = cell as! MyCustomCell
    myCell.label.font = UIFont.boldSystemFont(ofSize: 17.0)
}

注意:字体设置在willDisplayCell方法中的cell输入参数上,如上代码所示.如果使用 tableView.dequeueReusableCell(...)tableView 获取单元格,它将无法工作.

Note: that the font is set on the cell input parameter in the willDisplayCell method, as shown in the code above. If the cell is taken from the tableView using the tableView.dequeueReusableCell(...) it will not work.

这篇关于UILabel 的字体在视图重新出现之前不会调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆