Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom font not working programmatically in swift

Tags:

ios

swift

fonts

I've followed the step to to add custom fonts in xcode at swift day-by-day and custom fonts but I'm not able to set that font in app label programmatically.

var labeladd = UILabel(frame: CGRectMake(40, 50, 70, 22))
    //  label.center = CGPointMake(160, 284)
 ///  labeladd.font=UIFont.boldSystemFontOfSize(15)
   labeladd.font = UIFont(name:"Source Sans Pro",size:15)
    labeladd.textColor=UIColor.blackColor()
    labeladd.textAlignment = NSTextAlignment.Center
    labeladd.text = "this is custom fonts"
    myview.addSubview(labeladd)
like image 776
bLacK hoLE Avatar asked Nov 19 '14 00:11

bLacK hoLE


People also ask

How do I change the font style in Swift?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.

How do I register a font in iOS Swift?

Register Your Font File with iOS After adding the font file to your project, you need to let iOS know about the font. To do this, add the key "Fonts provided by application" to Info. plist (the raw key name is UIAppFonts ). Xcode creates an array value for the key; add the name of the font file as an item of the array.


7 Answers

I've found (XCode 12) that unless your font happens to be already used in your Main.storyboard, that even if you have done all the correct steps of registering the font file in your app, copied to your bundle resources, used the PostScript name, yadda, yadda, that it still didnt show up and trying to use it in UIFont(name: size:) would still return nil. :-(

I worked around this current bug (?) by manually registering all my custom fonts at startup in AppDelegate application(_ application: didFinishLaunchingWithOptions:)

let fonts = Bundle.main.urls(forResourcesWithExtension: "ttf", subdirectory: nil)
fonts?.forEach({ url in
    CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)
})

(adapted from Xcode: Using custom fonts inside Dynamic framework)

like image 21
tiritea Avatar answered Oct 02 '22 18:10

tiritea


Let's say you want to add this font: SourceSansPro-Regular.otf

Four steps:

  1. Add the font file SourceSansPro-Regular.otf to your project, make sure you select your target in the "Add to targets".

enter image description here

Go to the target's Build Phases and make sure it is under Copy Bundle Resources. If not, add it.

enter image description here



2. Go to the target's Info. Add a new entry Font provided by application then add a new item with this value SourceSansPro-Regular.otf.

enter image description here



  1. From your finder, double click on the file SourceSansPro-Regular.otf, it might ask you to install the font to your Font Book.



  1. Open the OS X Font Book application, navigate to your font then press Command+i. Note the PostScript name and use that name in your Swift code. In this case, it's SourceSansPro-Regular. enter image description here



So in your code:

labeladd.font = UIFont(name:"SourceSansPro-Regular", size:15)

like image 129
Van Du Tran Avatar answered Oct 03 '22 18:10

Van Du Tran


Sometimes the name of the font is not the name of your file. I was trying to use the NexaBook.otf font with the name "NexaBook" and at the end the problem was that the right name of the font is "Nexa-Book". To check you are using the right name write this code in the viewDidLoad and check the name of the font:

for family: String in UIFont.familyNames
    {
        print(family)
        for names: String in UIFont.fontNames(forFamilyName: family)
        {
            print("== \(names)")
        }
    }

In my case I got:

...
Nexa
== Nexa-Book
Avenir Next
== AvenirNext-Medium
== AvenirNext-DemiBoldItalic
== AvenirNext-DemiBold
...

I get this idea from Common Mistakes With Adding Custom Fonts to Your iOS App where you can find a great description of the problems working with custom fonts.

like image 23
abanet Avatar answered Oct 04 '22 18:10

abanet


enter image description here (1)First select the "Font" folder then make sure it's selected.

like image 20
HafizAnser Avatar answered Oct 01 '22 18:10

HafizAnser


If you are getting the font in storyboard but not in font family and are not able to set it progammatically, then the reason is that the Xcode might not have compiled the font. To solve it:

  1. Set the required font(say: Roboto Bold) as the font of any UI element on storyboard E.g UILabel.
  2. Run the project
  3. Now check the font family names. The required font(say: Roboto Bold) name would have appeared in it.
  4. Now you can set the required font(say: Roboto Bold) progammatically :)
like image 27
Shivani Bajaj Avatar answered Oct 05 '22 18:10

Shivani Bajaj


I was struggling with the issue quite a long time. It turned out that I forgot to add a .ttf / .otf extension to the font name in Info.plist.

like image 32
Bartosz Olszanowski Avatar answered Oct 05 '22 18:10

Bartosz Olszanowski


Follow Apple Guide to:

Adding a Custom Font to Your App

Beyond this, I've found that if your font is not already present in a Storyboard item, UIFont.familyNames.sorted() won't list it.

The workaround is to make a storyboard, you can leave unused, and add there a UILabel for each custom font you want to be available programmatically, and then set your font as custom font from properties panel.

This is not clean solution, but I didn't find any other solution.

Best regards.

/GM

like image 27
Giuseppe Mazzilli Avatar answered Oct 02 '22 18:10

Giuseppe Mazzilli