Collin O.
Collin O.

Reputation: 187

How to Change Font Style in Swift

I'm trying to figure out how to change the style of a font to "Thin". Does anyone know how to do this?

Here's my best try, but it doesn't work:

m.font = UIFont(name: "Apple SD Gothic Neo", style: "Thin", size: 8.0)

Upvotes: 15

Views: 32384

Answers (6)

LukeSideWalker
LukeSideWalker

Reputation: 7910

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()

Upvotes: 4

Pixel Code
Pixel Code

Reputation: 89

let myLabel = UILabel(frame: CGRect(x: 0, y: 0, width: yourWidth, height: yourHeight))
    myLabel.text = "Your Text"
    myLabel.font = UIFont(name: "Name of your font", size: 18)
    self.view.addSubview(emptyMessageLabel)
    myLabel.translatesAutoresizingMaskIntoConstraints = false
    myLabel.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
    myLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

Upvotes: 1

Caleb Kleveter
Caleb Kleveter

Reputation: 11494

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.

Upvotes: 31

Urvish Modi
Urvish Modi

Reputation: 1138

lblDes.font = UIFont (name: "HelveticaNeue-UltraLight", size: 14.0)

Upvotes: 1

Oleg Gordiichuk
Oleg Gordiichuk

Reputation: 15512

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)

Upvotes: 13

Niall Kehoe
Niall Kehoe

Reputation: 416

This might work:

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

Upvotes: 4

Related Questions