![](/img/trans.png)
[英]What is the alternative for adjustsFontForContentSizeCategory iOS?
[英]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 的字體大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.