使用 Swift 5 和 iOS 12,您可以选择以下三种解决方案之一来解决问题。
#1. 使用 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)
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. 使用UIContentSizeCategory
的didChangeNotification
类型属性
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)
NotificationCenter.default.addObserver(self, selector: #selector(preferredContentSizeChanged(_:)), name: UIContentSizeCategory.didChangeNotification, object: nil)
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)
}
}
#3. 使用UITraitCollection
的preferredContentSizeCategory
属性
UITraitCollection
有一个叫做preferredContentSizeCategory
的属性。该属性声明如下:
用户偏好的字体大小选项。
var preferredContentSizeCategory: UIContentSizeCategory { get }
使用动态类型,用户可以请求应用程序使用比系统定义的正常字体大小更大或更小的字体显示文本。例如,视力障碍的用户可能会要求更大的默认字体大小,以便更容易地阅读文本。使用此属性的值来请求与用户所需大小匹配的UIFont对象。下面的UIViewController实现展示了如何检测并响应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)
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)
}
}
}
资料来源:
if traitCollection.preferredContentSizeCategory.isAccessibilityCategory { /* ... */ }
。更多细节请参阅 useyourloaf.com / Making Space For Dynamic Type。 - Imanou Petit