Wednesday, March 29, 2017

Using Custom Fonts in Swift 3.0

Using Custom Fonts in Swift Code

 We could set the custom font in code.

To use a custom font we need to refer to it by name. But the name often isn’t the same as the font’s file name. There are 2 ways to find the name:
  1. Install the font on your Mac. Open Font Book, open the font and see what name is listed.
  2. Programmatically list the available fonts in your app.

    means follow below step,
    1.include the fonts in your project
    2. include the fonts in build phase,
    3. include install the font.
    4. RESTART the xcode
    5. and finally, choose your custom font, make sure to make it plain and not attributed. Select "CUSTOM" to find the custom font and not "SYSTEM".

Include your iOS custom fonts in your application Plist
Open it and add a new row called “Fonts provided by application” which will be an array that you need to add all the filenames of the fonts you want to use.



Find the name of the font
// MARK: - UIFont Extension

extension UIFont {
    func Light(_ size: CGFloat) -> UIFont {
        return UIFont(name: "Roboto-Light", size: getSize(size))!
    }
    func Regular(_ size: CGFloat) -> UIFont {
        return UIFont(name: "Roboto-Regular", size: getSize(size))!
    }
    func Medium(_ size: CGFloat) -> UIFont {
        return UIFont(name: "Roboto-Medium", size: getSize(size))!
    }
    func Bold(_ size: CGFloat) -> UIFont {
        return UIFont(name: "Roboto-Bold", size: getSize(size))!
    }
    func getSize (_ size: CGFloat) -> CGFloat {
        var sizeMain = size
        if Screen.isIphone6Plus {
            sizeMain = sizeMain + 2
        }
        else if Screen.isIphone6 {
            sizeMain = sizeMain + 1
        }
        return CGFloat(sizeMain)
    }
}

Below is the use of category.
        lblTitle.font = UIFont().Regular(16)
        lblPrice.font = UIFont().Medium(13)
        btnPrice.titleLabel!.font = UIFont().Medium(13)
        lblAuthorName.font = UIFont().Regular(12)
        lblSizeNote.font = UIFont().Regular(12)
        lblAuthorNameStatic.font = UIFont().Medium(13)
        lblSizeNoteStatic. font = UIFont (). Medium (13)
Enjoy

No comments:

Post a Comment