Custom Fonts only available when set in Interface Builder

前端 未结 6 1134
暖寄归人
暖寄归人 2021-01-13 08:50

I have added a custom font to my project. It is included in the target, and it is added in the plist. When I try to use it programmatically it doesn\'t work, and it doesn\'t

6条回答
  •  情歌与酒
    2021-01-13 09:41

    When I try to use it programmatically it doesn't work, and it doesn't show up when I print out a list of available fonts

    This proves that you have not in fact included it properly in the target and the Info.plist.

    The reason it seems to work in IB is that this font is also present on your computer. But in fact if you were to run this app on your device, you would see that even setting the font in IB is not working.

    Your font is Special Elite. As you can see, I have it visible here in my running app:

    enter image description here

    Here's the code that I used:

        let lab = UILabel()
        lab.text = "This is a test"
        lab.font = UIFont(name:"SpecialElite-Regular", size:18)
        lab.sizeToFit()
        lab.frame.origin = CGPointMake(100,100)
        self.view.addSubview(lab)
    

    So you see, it is possible to refer to this font in code — if it is loaded properly. You are evidently not loading it properly. It's not in your app bundle, or it's not in the copy build phase, or it's not correctly listed in your Info.plist.

    (Of course there's always a possibility that you're calling [UIFont fontWithName:size:] with a bad value for the name or for the size.)

提交回复
热议问题