Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 替换应用程序中每个UILabel的字体_Ios_Swift_Uifont_Swift4.2 - Fatal编程技术网

Ios 替换应用程序中每个UILabel的字体

Ios 替换应用程序中每个UILabel的字体,ios,swift,uifont,swift4.2,Ios,Swift,Uifont,Swift4.2,我正试图通过对UILabel进行扩展来更改字体 extension UILabel{ var defaultFont: UIFont? { get { return self.font } set { let oldFontSize = self.font.pointSize let oldFontName = self.font.fontName let newFontName =

我正试图通过对UILabel进行扩展来更改字体

extension UILabel{
    var defaultFont: UIFont? {
        get { return self.font }
        set {
            let oldFontSize = self.font.pointSize
            let oldFontName = self.font.fontName
            let newFontName  = newValue?.fontName
            self.font = UIFont(name: newFontName!, size: oldFontSize)  
        }
    }
}
然后打电话

UILabel.appearance().defaultFont = UIFont.init(name: "My Font", size: 5)
但是总是
self.font
nil
使用xcode 10 swift 4.2

编辑: 现在,在将swift版本更改为Swift3之后,它运行良好,然后问题出现在Swift4中

有解决方案或替代方法吗?

试试以下方法: 是appDelegate->didFinishLaunch:

UILabel.appearance().font = UIFont("your font name", size: 15)

标准方法是创建自定义的
UILabel
类,并在需要默认字体的任何地方使用该类。e、 g,

class MyFontLabel: UILabel {

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.commonInit()
    }

    private func commonInit() {
        self.font = UIFont(name: "My Font", size: self.font.pointSize)
    }
}

didfishlaunchingwithoptions
方法中,将此项设置为
appDelegate

UILabel.appearance().font = UIFont(name: "your font name", size: UILabel.appearance().font.pointSize)
所以,从技术上讲,重写UILabel的字体变量是行不通的,但可以扩展UILabel

public class CustomLabel: UILabel {
    override public var font: UIFont! {
        get { return super.font }
        set {
            super.font = UIFont(name: "Font-Name", size: super.font.pointSize)
        }
    }
}

我发现在UILabel的扩展中添加一个
样式(as style:TypographyStyle)
函数非常有用,其中TypographyStyle是我定义的一个枚举,它包含一系列样式属性,包括字体。我还为UILabel创建了一个便利init,它采用排版样式,并调用
样式(as:)
,以便在创建标签时自动应用样式

extension UILabel {
    convenience init(style: TypographyStyle) {
        self.init()

        self.style(as: style)
    }

    func style(as style: TypographyStyle) {
        self.font = UIFont(name: style.fontName, size: style.fontSize)
    }
}

enum TypographyStyle {
    case body, h1, h2, h3

    var font: UIFont {
        switch self {
        // here you can return different fonts for the different styles if you like
        default:
            return UIFont(name: "AvenirNext", size: 16)
        }
    }
}

// Usage
let label = UILabel(style: .body)

然后您应该创建两个扩展UiLabel的类。并使用这些设置字体和大小