Swift 4 iOS-完全覆盖系统字体,而不影响故事板字体大小
我想:Swift 4 iOS-完全覆盖系统字体,而不影响故事板字体大小,ios,swift,xcode,fonts,swift4.2,Ios,Swift,Xcode,Fonts,Swift4.2,我想: 覆盖所有应用程序中的默认字体,从情节提要中选择字体大小 如果可能,请在脚本中显示的文本中查看自定义字体的预览。 我设法插入了自定义字体,但文本大小有问题 我尝试了以下解决方案,但仍然不完全符合我的要求: 在didFinishLaunchingWithOptions func:UILabel.appearance()中的AppDelegate中添加了此行。font=UIFont(名称:“yourFont”,大小:yourSize) 这会覆盖所有UILabel的字体,但也会覆盖字体大小。 所
UILabel.appearance()中的AppDelegate
中添加了此行。font=UIFont(名称:“yourFont”,大小:yourSize)
这会覆盖所有UILabel的字体,但也会覆盖字体大小。 所有标签都具有相同的
yourSize
font sizeextension UILabel {
override open func awakeFromNib() {
super.awakeFromNib()
changeFontName()
}
func changeFontName() {
self.font = UIFont(name: "yourFont", size: self.font.pointSize)
}
}
这是可行的,但情节提要显然不会更新视图。我不确定这样做是否正确我采用的解决方案类似于
#2解决方案
,但它也会在故事板上呈现字体
我们创建了一些扩展默认UIView的类,例如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类
,用于每个必须更改字体的组件
它不是最干净的解决方案,但至少如果您在整个应用程序中更改MyCustomFont
字体,您只需一次代码更改即可