[英]How to detect Dynamic Font size changes from iOS Settings?
在 settings->general->text size 中,更改文本大小后,我必須退出我自己的應用程序才能將大小應用於
[UIFont preferredFontForTextStyle:..]
是否有委托或通知通知我的應用程序重新應用新尺寸?
更新:我嘗試了以下但有趣的是,字體大小將在我 BG 並啟動應用程序兩次后應用。
- (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];
}
您在UIContentSizeCategory上偵聽大小更改通知。
Swift 3.0: NSNotification.Name.UIContentSizeCategoryDidChange
Swift 4.0或更高版本: UIContentSizeCategory.didChangeNotification
使用Swift 5和iOS 12,您可以選擇以下三種解決方案之一來解決您的問題。
UIContentSizeCategoryAdjusting
的adjustsFontForContentSizeCategory
屬性 UILabel
, UITextField
和UITextView
符合UIContentSizeCategoryAdjusting
協議,因此具有名為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])
}
}
UIContentSizeCategory
的didChangeNotification
類型屬性 UIContentSizeCategory
有一個名為didChangeNotification
的type屬性。 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 */
}
}
UITraitCollection
的preferredContentSizeCategory
屬性 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 */
}
}
}
資料來源:
您也可以這樣做以獲得尺寸:
let contentSize = traitCollection.preferredContentSizeCategory
if contentSize.isAccessibilityCategory {
if contentSize >= .accessibilityLarge{
print("larger than or equal to accessibility large")
}else {
print("lower than large")
}
} else {
print("not accessibility")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.