iOS ttf fonts only partially work

冷暖自知 提交于 2019-12-30 10:33:07

问题


I need OpenSans in my app and so I imported the whole OpenSans bunch which includes Bold, ExtraBold, Italic, Regular, Light and more. I added them to Fontbook (to check the exact name I need to address them by when using them) on my Mac, to my project in the file structure and added them in Build phases to my project as well.

Now the weird thing; when I use them with UIFont, ONLY OpenSans-Bold works. The rest doesn't work. If I use OpenSans-Bold (exactly the identifier given by Fontbook), everything works fine. If I, however, change it to OpenSans-Regular or OpenSans-Light or something else in that family, I get the systemfont with UIFont and I get nothing when drawing with Quartz.

All fonts are in all lists and I checked all the identifiers from Fontbook (which was why I couldn't get OpenSans-Bold working), but now i'm at a loss. Any idea what I could be doing wrong?


回答1:


OpenSans Regular is just OpenSans, semi-bold is OpenSans-Semibold, etc.

In Font Book.app select the font and choose Command + I which brings up the font info. In the "PostScript name" in the top you can see what the font is called and how you can call it in your code.

Also, just make sure the font has actually been added in your bundle and your plist :)




回答2:


You Can check the fonts provided by the ttf file With the following lines of code..

this method is provided by the UIFont class.

  • (NSArray *)fontNamesForFamilyName:(NSString *)familyName



回答3:


You need to add the files in the right locations, then in the plist file, then enumerate them all and print them to get the PDF names so you can access them. Most likely you're using the wrong names. Fonts have three kinds of names:

  • The file name
  • The font name in Font Book
  • The PDF name - this is the one you use to access the font in code

See this answer: Adding custom fonts to iOS app finding their real names



来源:https://stackoverflow.com/questions/18021945/ios-ttf-fonts-only-partially-work

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