How to Change Font Style in Swift

You have trouble with font name.

At first find out proper name of the font and than use it.

Firstly print all names of them. And then use. Code example show all installed fonts of the application.

func printFonts() {
    let fontFamilyNames = UIFont.familyNames()
    for familyName in fontFamilyNames {
        print("------------------------------")
        print("Font Family Name = [\(familyName)]")
        let names = UIFont.fontNamesForFamilyName(familyName)
        print("Font Names = [\(names)]")
    }
}

And after you detect Font you can set this like :

m.font = UIFont(name: "AppleSDGothicNeo-Thin", size: 8.0)

This might work:

let font = UIFont(name: "HelveticaNeue-Thin", size: 16.0)!

The way I have seen it is AppleSDGothicNeo-Thin, No spaces, and a dash style. So your code would be

m.font = UIFont(name: "AppleSDGothicNeo-Thin", size: 8.0)

Edit:

I have come to understand why you use the font this way.

If you add a custom font to your project, it has a name of "SuperAwesomeFont-Light.ttf". So it makes sense that you just use the file name for the name of the font.


Put this in playground to get all correct names of the fonts, available (updated for Swift 3.0 up to Swift 5.0 on basis of oleg)

//: Playground - noun: a place where people can play

import UIKit

func printFonts() {
    let fontFamilyNames = UIFont.familyNames
    for familyName in fontFamilyNames {
        print("------------------------------")
        print("Font Family Name = [\(familyName)]")
        let names = UIFont.fontNames(forFamilyName: familyName)
        print("Font Names = [\(names)]")
    }
}

printFonts()

Tags:

Ios

Swift

Uifont