【问题标题】:What is the purpose of adjustsFontForContentSizeCategory in iOS 10?iOS 10 中adjustsFontForContentSizeCategory 的用途是什么?
【发布时间】:2017-03-23 02:36:23
【问题描述】:

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

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

【问题讨论】:

    标签: ios fonts accessibility dynamic-type-feature


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        动态类型仅适用于已实现text styles 的文本。

        因此,如果您总是想禁用动态类型并在单元格中显示相同大小的类型,请不要在其中使用文本样式或image size adjustment

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

        当您使用设置更改内容大小时,表格视图单元格不需要这个最新属性,这要归功于它们的 self-sizing feature。这就是为什么您在没有明确执行此操作的情况下注意到此行为的原因。

        【讨论】:

          猜你喜欢
          • 2011-12-16
          • 2012-06-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-27
          • 2010-09-27
          • 2011-12-17
          相关资源
          最近更新 更多