Custom Font Not Working Programmatically in Swift

Swift 4 set custom font programmatically

You can try

lbl.font = UIFont(name:"FontAwesome",size:15)

the name should be the font name as it is when you install it . not as the file name in your case was Noto Kufi Arabic instead of NotoKufiArabicRegular

click the font and open it with Font Book , then install it after that specify exactly the name shown in the parameter in the line above

Custom font not loading in SwiftUI project after moving code to another project?

Although it looks like you have already done this, just go through the following checklist once:

  1. Fonts (.ttf files) are present inside the project
    font files location
  2. Entry in info.plist file
    Sample Image
  3. Entry in "Copy Bundle Resources" under "Build Settings" of your target
    Sample Image
    If this is missing, add by clicking on the plus (+) icon in this section
  4. Make sure you are using the font correctly (check spelling errors?)
var FontRegular : Font = Font.custom("Poppins-Regular", size: 16)
var FontBold : Font = Font.custom("Poppins-Bold", size: 16)

...

Text("Sample Text")
.font(FontRegular)

SwiftUI won't display custom font

If you have made sure that the Info.plist is using the correct filename:

Sample Image

Note if you are using Xcode 13 you may not have an Info.plist where you expect. This SO answer explains where you can find it.

That the font is available in the app's target.

Sample Image

You also need to make sure that you are accessing the font by the correct name.

An easy way to check the font's name is to add the following to your AppDelegate in the didFinishLaunchingWithOptions before the return true. Or if you are using the new SwiftUI lifecycle you can add it to an .onAppear.

for family in UIFont.familyNames.sorted() {
let names = UIFont.fontNames(forFamilyName: family)
print("Family: \(family) Font names: \(names)")
}

This will list all the fonts by family and name.

Just remember to remove it once you have finished using it as you don't need to unnecessarily print to the console.

When I do it for my fonts (I have added the same font as you) I find the following in the console in the list of available fonts (see the above screenshot) :

Family: Helvetica Now Display Font names: ["HelveticaNowDisplay-Bold"]

Your font may have a different name to mine, and it is important to note that the font name may not be the same as the filename. This is what trips up a lot of people, as they try using the filename when they need to use the font name.

The following test code produces:

struct ContentView: View {
var body: some View {
Text("Hello")
.foregroundColor(.blue)
.font(Font.custom("HelveticaNowDisplay-Bold", size: 60))
}
}

Sample Image

For more information about adding custom fonts see Apple's documentation.



Dynamic Type in SwiftUI

If you are using a custom font then you should consider setting it up so that it will scale with dynamic type.

iOS 14

iOS 14 introduces a new modifier that allows you to scale a font relative to a Dynamic Font Type.

Text("Hello")
.font(.custom("HelveticaNowDisplay-Bold", size: 60, relativeTo: .body))

iOS 13

If you are using iOS 13 that requires a bit more effort to get the same effect.

You first need to create a ViewModifier. This view modifier listens to the size category from the environment (it doesn't actually use it but having it here makes sure the view modifier is updated whenever the size category is updated).

struct ScaledFont: ViewModifier {
@Environment(\.sizeCategory) var sizeCategory
var name: String
var size: CGFloat

func body(content: Content) -> some View {
let scaledSize = UIFontMetrics.default.scaledValue(for: size)
return content.font(.custom(name, size: scaledSize))
}
}

extension View {
func scaledFont(name: String, size: CGFloat) -> some View {
return self.modifier(ScaledFont(name: name, size: size))
}
}

It is then used in the following way:

Text("Hello")
.scaledFont(name: "HelveticaNowDisplay-Bold", size: 60)

For a really good write up check out this post on Hacking With Swift.

Custom font seen in Interface builder but not in code in iOS project

You're going to kick yourself...

In your plist info file, you have:

`Quicksand-Dash.otf`

but the file you added is named:

`Quicksand_Dash.otf`

underscore not hyphen

Either rename your file, or edit the plist entry, and you'll see your font.

Debug console output:

family: Quicksand
name: Quicksand-BoldItalic
name: QuicksandDash-Regular
name: Quicksand-Bold

Here is a plain single-view project that works fine for me - on both Simulator and Device. (I have not added the fonts to OS X, only to my project):

https://github.com/DonMag/Quicksand

Just for setup reference:

Sample Image

Sample Image



Related Topics



Leave a reply



Submit