custom font not working programmatically in swift

旧城冷巷雨未停 提交于 2019-11-26 17:41:26

问题


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)

回答1:


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

Three steps:

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

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



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.



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

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



So in your code:

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




回答2:


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.




回答3:


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




回答4:


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 :)



回答5:


If you are adding a custom font within a framework, you need to manually load the font in app delegate before using it.

Xcode: Using custom fonts inside Dynamic framework



来源:https://stackoverflow.com/questions/27006772/custom-font-not-working-programmatically-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!