4

I'm making an iOS app by Swift. I want to manage all font size and its style at one place like the below HTML code. In the below code, all I have to resize all fonts in the page is changing body{font-size}.

<style>
body{
    font-size: 30px; /* change only here */
}
</style>

<body>
    <div style="font-size: 1em">normal</div>
    <div style="font-size: 2em">double</div>
</body>

I would like to know how to manage all fonts in an APP in iOS development.

ryo
  • 2,009
  • 5
  • 24
  • 44

1 Answers1

4

You could do something like the following:
Atleast you can get some ideas on how you could accomplish this :)

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")
    }
}

Usage:

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
Laffen
  • 2,753
  • 17
  • 29
  • Thank you very much for responding. You answer is very helpful for me! Thank you. – ryo May 10 '16 at 11:50
  • Where is the initializer CustomFont(fontFamily: UIString) defined? – vikzilla Jan 30 '18 at 02:22
  • Struct types have something that's called Memberwise Initializers, take a look at [Classes and Structures](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html) – Laffen Jan 30 '18 at 10:07