web-dev-qa-db-ja.com

iOS設定から動的フォントサイズの変更を検出する方法

設定->一般->テキストサイズの中で、テキストサイズを変更した後、自分のアプリを終了してサイズを適用する必要があります

 [UIFont preferredFontForTextStyle:..]

アプリに新しいサイズを再適用するよう通知するデリゲートまたは通知はありますか?

更新:次のことを試しましたが、興味深いことに、フォントサイズは、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];

}
36
mskw

IContentSizeCategory でサイズ変更通知をリッスンします。

Swift 3.0:NSNotification.Name.UIContentSizeCategoryDidChange

Swift 4.0以降:UIContentSizeCategory.didChangeNotification

48
Dave DeLong

Swift 5およびiOS 12では、問題を解決するために3つの次のソリューションのいずれかを選択できます。


#1。 UIContentSizeCategoryAdjustingadjustsFontForContentSizeCategoryプロパティを使用する

UILabelUITextFieldおよびUITextViewUIContentSizeCategoryAdjustingプロトコルに準拠しているため、 adjustsFontForContentSizeCategory というインスタンスプロパティがあります。 adjustsFontForContentSizeCategoryには次の宣言があります。

デバイスのコンテンツサイズカテゴリが変更されたときにオブジェクトがフォントを自動的に更新するかどうかを示すブール値。

var adjustsFontForContentSizeCategory: Bool { get set }

以下のUIViewControllerの実装は、adjustsFontForContentSizeCategoryを使用してiOS設定で動的なフォントサイズの変更を検出して対応する方法を示しています。

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])
    }

}

#2。 UIContentSizeCategorydidChangeNotificationタイププロパティを使用する

UIContentSizeCategoryには didChangeNotification というタイププロパティがあります。 didChangeNotificationには次の宣言があります。

ユーザーが優先コンテンツサイズの設定を変更したときに投稿されます。

static let didChangeNotification: NSNotification.Name

この通知は、preferredContentSizeCategoryプロパティの値が変更されたときに送信されます。通知のuserInfoディクショナリには、新しい設定を反映するnewValueUserInfoKeyキーが含まれています。

以下のUIViewControllerの実装は、didChangeNotificationを使用してiOS設定で動的なフォントサイズの変更を検出して対応する方法を示しています。

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 */
    }

}

#3。 UITraitCollectionpreferredContentSizeCategoryプロパティを使用する

UITraitCollectionには preferredContentSizeCategory というプロパティがあります。 preferredContentSizeCategoryには次の宣言があります。

ユーザーが希望するフォントサイズ変更オプション。

var preferredContentSizeCategory: UIContentSizeCategory { get }

ダイナミックタイプを使用すると、システムで定義された通常のフォントサイズよりも大きいまたは小さいフォントを使用してテキストを表示するようアプリに要求できます。たとえば、視覚障害のあるユーザーは、テキストを読みやすくするために、デフォルトのフォントサイズを大きくするよう要求する場合があります。このプロパティの値を使用して、ユーザーの要求サイズと一致するUIFontオブジェクトを要求します。

以下のUIViewControllerの実装は、preferredContentSizeCategoryを使用してiOS設定で動的なフォントサイズの変更を検出して対応する方法を示しています。

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 */
        }
    }

}

出典:

38
Imanou Petit