gpt4 book ai didi

ios - iOS 10 中 adjustsFontForContentSizeCategory 的用途是什么?

转载 作者:可可西里 更新时间:2023-11-01 03:30:23 26 4
gpt4 key购买 nike

我正在设置 adjustsFontForContentSizeCategory在标准 UITableViewCelltextLabel 上。当我转到“设置”、“常规”、“辅助功能”、“较大的文本”以更改字体大小然后返回到我的应用程序时,UITableViewCellUILabel > 做相应的改变。这不应该发生,不是吗?

adjustsFontForContentSizeCategory 的确切用途是什么?如何防止 UITableViewCell 的标签更改其字体大小?

最佳答案

在讨论 TableView 之前,让我们讨论一下 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
}
}

好的,话虽如此, TableView 会自动观察 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
}
}

如您所见,我唯一需要做的就是将字体设置为动态文本,表格会自动适当更新。根据我的经验,在 TableView 中,不需要 adjustsFontForContentSizeCategory(看起来 TableView 本身必须观察必要的通知),但如果您没有遇到这种情况,您可以随时设置它自动调整大小行为。

如果您明确不希望表格 View 单元格的标签字体发生变化,那么就不要使用动态文本,例如:

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
}

关于ios - iOS 10 中 adjustsFontForContentSizeCategory 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40504348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com