内部设置 - > general->文字大小,在更改文字大小后,我必须退出自己的应用以将尺寸应用于
[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];
}
答案 0 :(得分:38)
您在UIContentSizeCategory上收听尺寸变更通知。
Swift 3.0:
NSNotification.Name.UIContentSizeCategoryDidChange
Swift 4.0或更高版本:
UIContentSizeCategory.didChangeNotification
答案 1 :(得分:27)
根据您的需求,使用Swift 4.2和iOS 12,您可以选择以下三个解决方案之一来解决您的问题。
UIContentSizeCategoryAdjusting
adjustsFontForContentSizeCategory
属性自iOS10起,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])
}
}
UIApplication
UIContentSizeCategoryDidChange
类型属性自iOS 7起,UIApplication
具有名为UIContentSizeCategoryDidChange
的类型属性。 UIContentSizeCategoryDidChange
有以下声明:
当用户更改首选内容大小设置时发布。
static let UIContentSizeCategoryDidChange: NSNotification.Name
此通知在值中发送
preferredContentSizeCategory
属性更改。通知的userInfo
字典包含UIContentSizeCategoryNewValueKey
密钥,该密钥反映了新设置。
下面的UIViewController
实现演示了如何使用UIContentSizeCategoryDidChange
检测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
属性自iOS 10起,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 */
}
}
}
来源: