iOS 10中adjustsFontForContentSizeCategory的用途是什么?

问题描述:

我在标准UITableViewCell的textLabel上设置了adjustsFontForContentSizeCategory。当我进入“设置”,“常规”,“辅助功能”,“大文本”来更改字体大小,然后返回到我的应用程序时,UITableViewCell的UILabels会相应地改变。这不应该发生,应该吗?iOS 10中adjustsFontForContentSizeCategory的用途是什么?

adjustsFontForContentSizeCategory的目的究竟是什么,以及如何防止UITableViewCells标签更改其字体大小?

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

在讨论表格视图之前,我们来讨论一下adjustsFontForContentSizeCategory。这样做的目的是控件会自动为我们调整字体。在此之前,您必须手动添加UIContentSizeCategoryDidChangeNotification的观察者。

因此,例如,在斯威夫特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: .UIContentSizeCategoryDidChange, object: nil, queue: .main) { [weak self] notification in 
      self?.dynamicTextLabel.font = .preferredFont(forTextStyle: .body) 
     } 
    } 

    deinit { 
     NotificationCenter.default.removeObserver(self, name: .UIContentSizeCategoryDidChange, 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 
    } 
} 

OK,话虽如此,表视图观察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 
}