What is the purpose of adjustsFontForContentSizeCategory in iOS 10?

I am setting adjustsFontForContentSizeCategory to the textLabel of a standard UITableViewCell. When I go to "Settings", "General", "Accessibility", "Big Text" to change the font size and then return to my application, the UILabels UITableViewCell change accordingly. This should not be, right?

What exactly is the purpose of adjustsFontForContentSizeCategory and how can I prevent UITableViewCells shortcuts from changing font size?

+7
source share
3 answers

, adjustsFontForContentSizeCategory. , . 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: .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
    }
}

, , , 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
}
+7

adjustsFontForContentSizeCategory UILabel.

0

.

, , .

@User511 @Rob, adjustsFontForContentSizeCategory , (, ).

When you use the settings for resizing content, the table view cells do not need this last property due to its automatic resizing function . This is the reason why you noticed this behavior while you didn’t do anything explicitly to achieve this goal.

0
source

Source: https://habr.com/ru/post/1660272/


All Articles