我想要:
我设法插入自定义字体,但我在文本大小的问题。
我尝试了以下解决方案,但他们仍然不完全符合我的要求:
AppDelegate
该行:
UILabel.appearance().font = UIFont(name: "yourFont", size: yourSize)
这将覆盖字体为所有UILabels但它覆盖的字体大小了。所有标签带有相同yourSize
字体大小。extension UILabel {
override open func awakeFromNib() {
super.awakeFromNib()
changeFontName()
}
func changeFontName() {
self.font = UIFont(name: "yourFont", size: self.font.pointSize)
}
}
这是工作,但情节串连图板显然不更新视图。而且我不知道它是做它的正确方法该解决方案我走近它类似于#2 solution
,但它呈现的故事板的字体了。
我们创造了一些类,扩展默认UIViews,例如用于UILabel
和UIButton
。你可以这样做:
@IBDesignable
public class CustomUILabel: UILabel {
public override func awakeFromNib() {
super.awakeFromNib()
configureLabel()
}
public override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
configureLabel()
}
func configureLabel() {
font = UIFont(name: "MyCustomFont", size: self.font.pointSize)
}
}
@IBDesignable
public class CustomUIButton: UIButton {
public override func awakeFromNib() {
super.awakeFromNib()
configureLabel()
}
public override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
configureLabel()
}
func configureLabel() {
titleLabel?.font = UIFont(name: "MyCustomFont", size: self.titleLabel!.font.pointSize)
}
}
然后在故事板,我们在Identity Inspector
该类作为Custom Class
设置为不得不改变字体的每个组件。
这是不是最干净的解决方案,但至少如果你改变了MyCustomFont
字体在整个应用程序,您只可以在一个单次改变它的代码。