3

TL;DR: Custom fonts couldn't be used programmatically before using them in a Storyboard/xib.

Note: I've checked out this, tried the answers and they didn't work. I've also made sure that they're in target membership.

I've noticed a strange bug while changing segment control title a custom font:

segmentedControl.titleTextAttributes = NSDictionary(objects: [UIFont.init(name: "Comfortaa-Regular",
                                                                          size: UIFont.systemFontSize)!,
                                                              UIColor.white],
                                                    forKeys: [NSAttributedStringKey.font as NSCopying,
                                                              NSAttributedStringKey.foregroundColor as NSCopying]) as? [AnyHashable : Any]

It couldn't find the font, so unwrapping has failed. But the font could be seen in the Storyboard.

FontList

It's properly added to the project, here's the CopyBundle and InfoList:

CopyBundleResources

InfoPlist

So here's the catch; if I use it in the Storyboard, it is shown in the font families:

ComfortaaBold

But if not, it's not shown -here I've changed to light and bold has disappeared-, and cannot be used programmatically.

ComfortaaLight

I'm using Xcode Version 9.0 (9A235), and Swift 4. I've also checked OpenRadar and couldn't find a submission regarding this.

The mentioned font: Comfortaa

EDUsta
  • 1,932
  • 2
  • 21
  • 30
  • I have the same problem with different font. And I also have checked all the steps multiple times. Font does not work unless added to storyboard somewhere. (Xcode 10) – b.zdybowicz Dec 21 '18 at 14:31

2 Answers2

2

@EDUsta, I just tried with given font and its work ok, no issue in it, giving the step which i followed:

  1. Add the Comfortaa-Bold.ttf, Comfortaa-Light.ttf, Comfortaa-Regular.ttf Font files to project.

enter image description here

2.Add the entries of Fonts in info.plist file

enter image description here

Make sure the font added in project target.

After it, apply the fonts on UILabel and UISegmentedControl using Swift code given below:

   let fontFirst = UIFont(name: "Comfortaa-Bold", size: 16.0)!
    segmentFont.setTitleTextAttributes([NSAttributedStringKey.font : fontFirst, NSAttributedStringKey.foregroundColor : UIColor.red],
                                       for: .normal)

    labelBold.font = UIFont(name: "Comfortaa-Bold", size: 16.0)
    labelLight.font = UIFont(name: "Comfortaa-Light", size: 16.0)
    labelRegular.font = UIFont(name: "Comfortaa-Regular", size: 16.0)

its work perfectly, you can check in below screenshot image:

enter image description here

For your reference i am adding the complete project here:

Community
  • 1
  • 1
Rocky
  • 2,903
  • 1
  • 22
  • 26
  • I've realized that Fonts Provided By Application items needs their extension with the project you've provided! So it's fixed now, but I'm still curious that how it works after adding them to storyboards... – EDUsta Jan 16 '18 at 10:21
  • @EDUsta info.plist contains the list for font files which we provides to App, if we are adding the font file with font name like "Comfortaa-Bold.ttf", but not specify the same in info.plist file then it can't be load the same font by programatically, but using IB we providing the font by file name added to target, in this way no need to specify the list in info.plist file. – Rocky Jan 16 '18 at 10:46
1

I have the same problem. I can't add exactly the same font family (Comfortaa) programatically - everytime it crashes, but once I add label in launchscreen and set font to Comfotaa-Bold, font loaded from code works fine and doesn't crash. So my solution is to add 3 labels with fonts such as - Comfortaa Bold, Comfortaa Regular, Comfortaa Light in launchscreen and set "hidden" flag on true. This way I'm able to use all of them programatically.

mkkrolik
  • 1,200
  • 14
  • 21
  • I went with the same way, currently there are 10+ labels that I needed to use in Storyboard, but it's enough to continue for now. – EDUsta Jan 16 '18 at 10:06