簡體   English   中英

iOS 10 中 adjustsFontForContentSizeCategory 的目的是什么?

[英]What is the purpose of adjustsFontForContentSizeCategory in iOS 10?

我在標准 UITableViewCell 的 textLabel 上設置 adjustsFontForContentSizeCategory。 當我轉到“設置”、“常規”、“輔助功能”、“大文本”以更改字體大小然后返回到我的應用程序時,UITableViewCell 的 UILabels 會相應地更改。 這不應該發生,不是嗎?

adjustsFontForContentSizeCategory 的目的究竟是什么,如何防止 UITableViewCells 標簽改變其字體大小?

在我們討論表格視圖之前,讓我們討論adjustsFontForContentSizeCategory 這樣做的目的是控件會自動為我們調整字體。 在此之前,您必須手動為UIContentSizeCategory.didChangeNotification (以前稱為UIContentSizeCategoryDidChangeNotification )添加觀察者。

因此,例如,在 Swift 3 中,在 10 之前的 iOS 版本中,為了在用戶更改其首選字體大小時更新字體,我們必須執行以下操作:

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自動觀察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 的字體大小。

動態類型僅適用於已實現文本樣式的文本

因此,如果您總是想禁用動態類型並在單元格中顯示相同大小的類型,請不要在其中使用文本樣式或圖像大小調整

正如@User511 和@Rob 所解釋的,屬性adjustsFontForContentSizeCategory告訴系統自動處理它所屬對象的動態類型(當然必須使用文本樣式)

當您使用設置更改內容大小時,表格視圖單元格不需要此最新屬性,這要歸功於它們的自動調整大小功能 這就是為什么你注意到這種行為而你沒有明確地達到這個目的的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM