自定义字体在swift中无法以编程方式工作 [英] custom font not working programmatically in swift

查看:167
本文介绍了自定义字体在swift中无法以编程方式工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照步骤在xcode中添加自定义字体每天快速
自定义字体但我无法以编程方式在app标签中设置该字体。

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)


推荐答案

假设您要添加此字体: SourceSansPro-Regular.otf

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

三个步骤:


  1. 将字体文件 SourceSansPro-Regular.otf 添加到项目中,确保在添加到目标中选择目标。

  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.转到目标的信息。添加一个新条目由应用程序提供的字体,然后添加一个具有此值的新项目 SourceSansPro-Regular.otf



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.





3.打开OS X Font Book 应用程序,导航到您的字体,然后按 Command + i 。请注意 PostScript名称,并在Swift代码中使用该名称。在这种情况下,它是 SourceSansPro-Regular



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

< br>


所以在你的代码中:



So in your code:

labeladd.font = UIFont(名称:SourceSansPro-常规,大小:15)

这篇关于自定义字体在swift中无法以编程方式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆