自定义字体无法在

自定义字体无法在

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

问题描述

我已经按照步骤在 每天快速自定义字体 但我无法以编程方式在应用标签中设置该字体.

I've followed the step to to add custom fonts in xcode at swift day-by-day andcustom 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

四个步骤:

  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.



  1. 在您的查找器中,双击文件 SourceSansPro-Regular.otf,它可能会要求您将字体安装到您的字体簿中.
  1. From your finder, double click on the file SourceSansPro-Regular.otf, it might ask you to install the font to your Font Book.



  1. 打开 OS X Font Book 应用程序,导航到您的字体,然后按 Command+i.请注意 PostScript 名称 并在您的 Swift 代码中使用该名称.在本例中,它是 SourceSansPro-Regular.
  1. 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)

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

08-23 08:12