使用 Swift 5 和 iOS 12,您可以选择以下三种解决方案之一来解决您的问题。
#1。使用UIContentSizeCategoryAdjusting
的adjustsFontForContentSizeCategory
属性
UILabel
,UITextField
并UITextView
符合UIContentSizeCategoryAdjusting
协议,因此有一个名为 . 的实例属性adjustsFontForContentSizeCategory
。adjustsFontForContentSizeCategory
有以下声明:
一个布尔值,指示对象是否在设备的内容大小类别更改时自动更新其字体。
var adjustsFontForContentSizeCategory: Bool { get set }
下面的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])
}
}
#2。使用UIContentSizeCategory
的didChangeNotification
类型属性
UIContentSizeCategory
有一个名为 的类型属性didChangeNotification
。didChangeNotification
有以下声明:
当用户更改首选内容大小设置时发布。
static let didChangeNotification: NSNotification.Name
preferredContentSizeCategory
当属性中的值更改时发送此通知。通知的 userInfo 字典包含newValueUserInfoKey
反映新设置的键。
下面的UIViewController
实现展示了如何检测和响应 iOS 设置中的动态字体大小变化didChangeNotification
:
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。使用UITraitCollection
的preferredContentSizeCategory
属性
UITraitCollection
有一个名为 的属性preferredContentSizeCategory
。preferredContentSizeCategory
有以下声明:
用户首选的字体大小选项。
var preferredContentSizeCategory: UIContentSizeCategory { get }
使用动态类型,用户可以要求应用使用大于或小于系统定义的正常字体大小的字体显示文本。例如,有视觉障碍的用户可能会要求更大的默认字体大小,以便更容易阅读文本。使用此属性的值来请求UIFont
与用户请求的大小相匹配的对象。
下面的UIViewController
实现展示了如何检测和响应 iOS 设置中的动态字体大小变化preferredContentSizeCategory
:
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 */
}
}
}
资料来源: