如何从iOS设置中检测动态字体大小更改?

msk*_*skw 33 uifont ios

在设置内部 - >常规 - >文本大小,更改文本大小后,我必须退出自己的应用程序才能应用大小

 [UIFont preferredFontForTextStyle:..]
Run Code Online (Sandbox Code Playgroud)

是否有代表或通知通知我的应用重新应用新尺寸?

更新:我尝试了以下但有趣的是,字体大小将在我BG之后应用并启动应用程序TWICE.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fromBg:) name:UIApplicationDidBecomeActiveNotification object:nil];

}


 -(void) fromBg:(NSNotification *)noti{

    self.headline1.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
    self.subHeadline.font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
    self.body.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    self.footnote.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
    self.caption1.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1];
    self.caption2.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
//    [self.view layoutIfNeeded];

}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ong 45

你在听NSNotification.Name.UIContentSizeCategoryDidChange.

Swift 3.0: UIContentSizeCategory.didChangeNotification

  • 好的,但是我该怎么做才能应用新的字体大小?我是否再次将字体设置为 preferredFontForTextStyle? (2认同)

Ima*_*tit 33

使用Swift 4.2,根据您的需要,您可以选择以下三种解决方案之一来解决您的问题.


#1.使用UIContentSizeCategoryAdjusting adjustsFontForContentSizeCategory财产

由于iOS10, UILabel,UITextFieldUITextView符合UIContentSizeCategoryAdjusting协议,因此具有所谓的实例属性adjustsFontForContentSizeCategory.adjustsFontForContentSizeCategory有以下声明:

一个布尔值,指示当设备的内容大小类别更改时对象是否自动更新其字体.

var adjustsFontForContentSizeCategory: Bool { get set }
Run Code Online (Sandbox Code Playgroud)

UIViewController下面的实现显示了如何在iOS设置中检测并响应动态字体大小更改adjustsFontForContentSizeCategory:

import UIKit

class ViewController: UIViewController {

    let label = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        label.text = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        label.numberOfLines = 0
        label.font = .preferredFont(forTextStyle: UIFont.TextStyle.body)
        label.adjustsFontForContentSizeCategory = true
        view.addSubview(label)

        // Auto layout
        label.translatesAutoresizingMaskIntoConstraints = false
        let horizontalConstraint = label.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        let verticalConstraint = label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        let widthConstraint = label.widthAnchor.constraint(equalToConstant: 300)
        NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, widthConstraint])
    }

}
Run Code Online (Sandbox Code Playgroud)

#2.使用UIContentSizeCategory didChangeNotificationtype属性

从iOS 7开始,UIContentSizeCategory有一个名为的类型属性didChangeNotification.didChangeNotification有以下声明:

用户更改首选内容大小设置时发布.

static let didChangeNotification: NSNotification.Name
Run Code Online (Sandbox Code Playgroud)

preferredContentSizeCategory属性中的值更改时,将发送此通知 .该newValueUserInfoKey通知的词典中包含UIViewController的关键,这反映了新的设置.

didChangeNotification下面的实现显示了如何在iOS设置中检测并响应动态字体大小更改UITraitCollection:

import UIKit

class ViewController: UIViewController {

    let label = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        label.text = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        label.numberOfLines = 0
        label.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body)
        view.addSubview(label)

        // Register for `UIContentSizeCategory.didChangeNotification`
        NotificationCenter.default.addObserver(self, selector: #selector(preferredContentSizeChanged(_:)), name: UIContentSizeCategory.didChangeNotification, object: nil)

        // Auto layout
        label.translatesAutoresizingMaskIntoConstraints = false
        let horizontalConstraint = label.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        let verticalConstraint = label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        let widthConstraint = label.widthAnchor.constraint(equalToConstant: 300)
        NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, widthConstraint])
    }

    @objc func preferredContentSizeChanged(_ notification: Notification) {
        label.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body)
        /* perform other operations if necessary */
    }

}
Run Code Online (Sandbox Code Playgroud)

#3.使用preferredContentSizeCategory UITraitCollection财产

从iOS 10开始,preferredContentSizeCategory就有一个叫做的属性preferredContentSizeCategory.UIFont有以下声明:

用户首选的字体大小调整选项.

var preferredContentSizeCategory: UIContentSizeCategory { get }
Run Code Online (Sandbox Code Playgroud)

使用动态类型,用户可以要求应用程序使用大于或小于系统定义的普通字体大小的字体显示文本.例如,具有视觉障碍的用户可能会请求更大的默认字体大小,以便更容易阅读文本.使用此属性的值来请求UIViewController与用户请求的大小匹配的对象.

preferredContentSizeCategory下面的实现显示了如何在iOS设置中检测并响应动态字体大小更改UIContentSizeCategoryAdjusting:

import UIKit

class ViewController: UIViewController {

    let label = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        label.text = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        label.numberOfLines = 0
        label.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body)
        view.addSubview(label)

        // Auto layout
        label.translatesAutoresizingMaskIntoConstraints = false
        let horizontalConstraint = label.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        let verticalConstraint = label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        let widthConstraint = label.widthAnchor.constraint(equalToConstant: 300)
        NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, widthConstraint])
    }

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)

        if previousTraitCollection?.preferredContentSizeCategory != traitCollection.preferredContentSizeCategory {
            self.label.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body)
            /* perform other operations if necessary */
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

资料来源:

  • @Dave 你可以这样测试:`if traitCollection.preferredContentSizeCategory.isAccessibilityCategory { /* ... */ }`。有关更多详细信息,请参阅 [useyourloaf.com / 为动态类型创造空间](https://useyourloaf.com/blog/making-space-for-dynamic-type/)。 (3认同)