iOS 10中adjustsFontForContentSizeCategory的目的是什么?

13

我正在设置一个标准UITableViewCelltextLabel,并在其上设置adjustsFontForContentSizeCategory属性。当我到“设置”,“通用”,“辅助功能”,“更大的文字”中更改字体大小,然后返回到我的应用程序时,UITableViewCellUILabel会相应地更改。这种情况不应该发生,对吗?

adjustsFontForContentSizeCategory的作用是什么,如何防止UITableViewCell的标签更改字体大小?

3个回答

13
在讨论表格视图之前,让我们先谈一下adjustsFontForContentSizeCategory。其目的是控制会自动地为我们调整字体大小。在此之前,您必须手动添加一个观察器来监测UIContentSizeCategory.didChangeNotification(以前叫做UIContentSizeCategoryDidChangeNotification)。例如,在Swift 3中,在iOS 10之前的版本中,为了使字体能够跟随用户更改偏好的字体大小而更新,我们需要执行以下操作:
class ViewController: UIViewController {

    @IBOutlet weak var dynamicTextLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        dynamicTextLabel.font = .preferredFont(forTextStyle: .body)

        NotificationCenter.default.addObserver(forName: UIContentSizeCategory.didChangeNotification, object: nil, queue: .main) { [weak self] notification in
            self?.dynamicTextLabel.font = .preferredFont(forTextStyle: .body)
        }
    }

    deinit {
        NotificationCenter.default.removeObserver(self, name: UIContentSizeCategory.didChangeNotification, object: nil)
    }
}
在iOS 10中,我们可以使用adjustsFontForContentSizeCategory属性,不再需要观察者,从而将上述过程简化为:
class ViewController: UIViewController {

    @IBOutlet weak var dynamicTextLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        dynamicTextLabel.font = .preferredFont(forTextStyle: .body)
        dynamicTextLabel.adjustsFontForContentSizeCategory = true
    }
}

好的,话虽如此,表视图会自动观察 UIContentSizeCategoryDidChangeNotification。你是否看到文本大小调整取决于单元格标签是否使用了动态类型。如果你使用像下面这样的动态文本,你将看到表格在系统首选字体大小更改时更新(无需使用 adjustsFontForContentSizeCategory):

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // make sure the cell resizes for the font with the following two lines

        tableView.estimatedRowHeight = 44
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1000
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.font = .preferredFont(forTextStyle: .body)
        // cell.textLabel?.adjustsFontForContentSizeCategory = true
        cell.textLabel?.text = "Row \(indexPath.row)"
        return cell
    }
}

正如您所看到的,我所要做的就是将字体设置为动态文本,表格会自动适应更新。在我的经验中,在表视图中,adjustsFontForContentSizeCategory不是必需的(看起来表视图必须自己观察必要的通知),但如果您没有体验到自动调整大小的行为,您可以随时设置它。

如果您明确不希望表视图单元格标签的字体更改,则只需不使用动态文本,例如:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.font = .systemFont(ofSize: 17)
    cell.textLabel?.text = "Row \(indexPath.row)"
    return cell
}

0

基本上,adjustsFontForContentSizeCategory 动态地改变 UILabel 的字体大小。


0

动态类型仅适用于已实现文本样式的文本。

因此,如果您始终希望禁用动态类型并在单元格中显示相同大小的字体,请不要在其中使用文本样式或图像大小调整

正如@User511和@Rob所解释的那样,属性adjustsFontForContentSizeCategory告诉系统为其所属的对象自动处理动态类型(当然必须使用文本样式)

当您使用设置更改内容大小时,由于其自尺寸特性,表视图单元格不需要此最新属性。这就是为什么您注意到了这种行为,而您没有明确采取任何措施来实现此目的的原因。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接