Custom Fonts Bug

前端 未结 2 1442
猫巷女王i
猫巷女王i 2020-12-21 06:49

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 an

相关标签:
2条回答
  • 2020-12-21 07:33

    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.

    0 讨论(0)
  • 2020-12-21 07:34

    @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.

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

    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:

    For your reference i am adding the complete project here:

    0 讨论(0)
提交回复
热议问题