我正在使用UIFont来更改我的应用程序中的默认字体。该代码在iOS 10及以下版本上运行良好,但在iOS 11上返回零,没有任何变化。我用的是斯威夫特4号
这是我返回零的代码:
UIFont(name: "AIranianSans", size: 20)你能帮我吗?怎么了?
发布于 2017-10-09 05:14:05
试着检查可用字体
for familyName:String in UIFont.familyNames {
print("Family Name: \(familyName)")
for fontName:String in UIFont.fontNames(forFamilyName: familyName) {
print("--Font Name: \(fontName)")
}
}可能是字体拼写不同。
发布于 2017-10-09 06:00:03
它看起来像一种自定义字体,您必须将它作为XCode中的资源添加。XCode 9有一个已知的问题,即当您通过拖放方式包含资源时,不添加要针对的资源。
在“文件”检查器中检查.ttf文件的目标成员资格。

发布于 2017-12-16 20:23:08
在下面的步骤中提到https://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/之后,我无法在swift4运行时在应用程序中加载字体。字体将出现在运行时,当我们应用该特定的自定义字体在故事板。那么只有这个api
UIFont(name: "font name", size: 20.0) 返回字体实例,否则它将只返回零。因此,设计时间自定义字体工作良好,但不运行时。
要运行时,我们需要加载字体
加载您喜欢的字体文件
UIFont.registerFontWithFilenameString(filenameString: "font 1 Sans LF Bold.ttf", bundle: Bundle.main)
UIFont.registerFontWithFilenameString(filenameString: "font 2Sans LF Light.ttf", bundle: Bundle.main)
UIFont.registerFontWithFilenameString(filenameString: "font 3Sans LF Regular.ttf", bundle: Bundle.main)
UIFont.registerFontWithFilenameString(filenameString: "font 4Sans LF Medium.ttf", bundle: Bundle.main)公共静态函数registerFontWithFilenameString(filenameString:字符串,bundle: Bundle) {
guard let pathForResourceString = bundle.path(forResource: filenameString, ofType: nil) else {
print("UIFont+: Failed to register font - path for resource not found.")
return
}
guard let fontData = NSData(contentsOfFile: pathForResourceString) else {
print("UIFont+: Failed to register font - font data could not be loaded.")
return
}
guard let dataProvider = CGDataProvider(data: fontData) else {
print("UIFont+: Failed to register font - data provider could not be loaded.")
return
}
guard let fontRef = CGFont(dataProvider) else {
print("UIFont+: Failed to register font - font could not be loaded.")
return
}
var errorRef: Unmanaged<CFError>? = nil
if (CTFontManagerRegisterGraphicsFont(fontRef, &errorRef) == false) {
print("UIFont+: Failed to register font - register graphics font failed - this font may have already been registered in the main bundle.")
}https://stackoverflow.com/questions/46639130
复制相似问题