ios 自定义字体无法在 swift 中以编程方式工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27006772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
custom font not working programmatically in swift
提问by bLacK hoLE
I've followed the step to to add custom fonts in xcode at swift day-by-dayand custom fontsbut I'm not able to set that font in app label programmatically.
我已经按照步骤在 xcode 中快速添加自定义字体和 自定义字体,但我无法以编程方式在应用程序标签中设置该字体。
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)
回答by Van Du Tran
Let's say you want to add this font: SourceSansPro-Regular.otf
假设您要添加此字体: SourceSansPro-Regular.otf
Four steps:
四个步骤:
- Add the font file
SourceSansPro-Regular.otf
to your project, make sure you select your target in the "Add to targets".
- 将字体文件添加
SourceSansPro-Regular.otf
到您的项目中,确保在“添加到目标”中选择您的目标。
Go to the target's Build Phasesand make sure it is under Copy Bundle Resources. If not, add it.
转到目标的Build Phases并确保它位于Copy Bundle Resources 下。如果没有,请添加它。
2. Go to the target's Info. Add a new entry Font provided by applicationthen add a new item with this value SourceSansPro-Regular.otf
.
2. 转到目标的信息。添加应用程序提供的新条目Font然后添加具有此值的新项目SourceSansPro-Regular.otf
。
- From your finder, double click on the file
SourceSansPro-Regular.otf
, it might ask you to install the font to your Font Book.
- 从您的查找器中,双击该文件
SourceSansPro-Regular.otf
,它可能会要求您将字体安装到您的字体簿中。
- Open the OS X Font Bookapplication, navigate to your font then press Command+i. Note the PostScript nameand use that name in your Swift code. In this case, it's
SourceSansPro-Regular
.
- 打开 OS X Font Book应用程序,导航到您的字体,然后按Command+i。记下PostScript 名称并在 Swift 代码中使用该名称。在这种情况下,它是
SourceSansPro-Regular
.
So in your code:
所以在你的代码中:
labeladd.font = UIFont(name:"SourceSansPro-Regular", size:15)
labeladd.font = UIFont(name:"SourceSansPro-Regular", size:15)
回答by abanet
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:
有时字体的名称不是您的文件的名称。我试图使用名为“NexaBook”的 NexaBook.otf 字体,最后问题是字体的正确名称是“Nexa-Book”。要检查您使用的名称是否正确,请在 viewDidLoad 中编写此代码并检查字体名称:
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 Appwhere you can find a great description of the problems working with custom fonts.
我从将自定义字体添加到 iOS 应用程序的常见错误中得到这个想法,您可以在其中找到有关使用自定义字体的问题的详细描述。
回答by Shivani Bajaj
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:
如果您在故事板中获取字体但不在字体系列中并且无法以编程方式设置它,则原因是 Xcode 可能未编译该字体。要解决它:
- Set the required font(say: Roboto Bold) as the font of any UI element on storyboard E.g UILabel.
- Run the project
- Now check the font family names. The required font(say: Roboto Bold) name would have appeared in it.
- Now you can set the required font(say: Roboto Bold) progammatically :)
- 将所需的字体(例如:Roboto Bold)设置为故事板(例如 UILabel)上任何 UI 元素的字体。
- 运行项目
- 现在检查字体系列名称。所需的字体(例如:Roboto Bold)名称会出现在其中。
- 现在您可以以编程方式设置所需的字体(例如:Roboto Bold):)
回答by HafizAnser
回答by Mocha
If you are adding a custom font within a framework, you need to manually load the font in app delegate before using it.
如果要在框架中添加自定义字体,则需要在使用前手动加载应用程序委托中的字体。