我正在设置一个标准UITableViewCell
的textLabel,并在其上设置adjustsFontForContentSizeCategory
属性。当我到“设置”,“通用”,“辅助功能”,“更大的文字”中更改字体大小,然后返回到我的应用程序时,UITableViewCell
的UILabel
会相应地更改。这种情况不应该发生,对吗?
adjustsFontForContentSizeCategory
的作用是什么,如何防止UITableViewCell
的标签更改字体大小?
我正在设置一个标准UITableViewCell
的textLabel,并在其上设置adjustsFontForContentSizeCategory
属性。当我到“设置”,“通用”,“辅助功能”,“更大的文字”中更改字体大小,然后返回到我的应用程序时,UITableViewCell
的UILabel
会相应地更改。这种情况不应该发生,对吗?
adjustsFontForContentSizeCategory
的作用是什么,如何防止UITableViewCell
的标签更改字体大小?
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
}
基本上,adjustsFontForContentSizeCategory 动态地改变 UILabel 的字体大小。