IOS - 所有字体管理在一个地方

时间:2016-05-10 10:45:10

标签: ios swift

我正在通过Swift制作iOS应用。我想在一个地方管理所有字体大小及其样式,如下面的HTML代码。在下面的代码中,我必须调整页面中所有字体的大小才会改变SeekBar seekBar = (SeekBar) view.findViewById(R.id.seek);

body{font-size}

我想知道如何在iOS开发中管理APP中的所有字体。

1 个答案:

答案 0 :(得分:4)

您可以执行以下操作:
至少你可以得到一些关于如何实现这一目标的想法:)

extension UIFont {

    struct CustomFont {

        private var fontFamily: String!

        static let defaultFontSize: CGFloat = 12

        func Light(withSize size: CGFloat = CustomFont.defaultFontSize) -> UIFont? {
            return UIFont(name: "\(fontFamily)-Normal", size: size)
        }

        func Normal(withSize size: CGFloat = CustomFont.defaultFontSize) -> UIFont? {
            return UIFont(name: "\(fontFamily)-Normal", size: size)
        }

        func Bold(withSize size: CGFloat = CustomFont.defaultFontSize) -> UIFont? {
            return UIFont(name: "\(fontFamily)-Normal", size: size)
        }
    }

    class var Lato: CustomFont {
        return CustomFont(fontFamily: "Lato")
    }

    class var Helvetica: CustomFont {
        return CustomFont(fontFamily: "Helvetica")
    }

    class var ComicSans: CustomFont {
        return CustomFont(fontFamily: "ComicSans")
    }
}

用法:

let font = UIFont.Lato.Normal() // Returns a Normal font with default font size
let font = UIFont.Lato.Bold(withSize: 18) // Returns a Bold font with custom size