如何找到UILabel的现有UIFontTextStyle?

lau*_*one 5 iphone uilabel ios swift uifontdescriptor

我目前正在创建扩展,UILabel以方便观察动态类型。一旦UIContentSizeCategoryDidChangeNotification收到,我想我的选择通过使用设置标签的字体

self.font = UIFont.preferredFontForTextStyle(someUIFontTextStyle)
Run Code Online (Sandbox Code Playgroud)

其中,someUIFontTextStyle使用相同的UIFontTextStyle标签目前呈现。我曾希望可以通过类似这样的属性来访问这样的属性self.font.fontDescriptor.textStyle,但事实似乎有点令人费解。

有没有办法访问与UIFontTextStyle关联的属性UILabel

self.font.fontDescriptor().objectForKey(UIFontDescriptorTextStyleAttribute) as? String
Run Code Online (Sandbox Code Playgroud)

pbm*_*pbm 4

接受的答案对于 swift 3 来说不太正确。 UIContentSizeCategoryDidChangeNotification的处理程序需要执行以下操作:

if let font = myUILable.font.fontDescriptor.object(forKey: UIFontDescriptorTextStyleAttribute) as? UIFontTextStyle {
    myUILabel.font = UIFont.preferredFont(forTextStyle: font)
}
Run Code Online (Sandbox Code Playgroud)