ios 如何检查字体在iOS版本中是否可用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8529593/
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
How to check if a font is available in version of iOS?
提问by tg2007
I'm currently working on an app that uses the "ChalkboardSE-Regular" font and my deployment target is 4.0+. This font was not available in 4.1 but it is supported in 4.3. What would be the best way to go about checking if the font exists and if it does not, use another supported font on the <4.1 versions of iOS. [UIFont familyName] returns a list of these fonts "Chalkboard SE"
我目前正在开发一个使用“ChalkboardSE-Regular”字体的应用程序,我的部署目标是 4.0+。此字体在 4.1 中不可用,但在 4.3 中受支持。检查字体是否存在的最佳方法是什么,如果不存在,请在 <4.1 版本的 iOS 上使用另一种受支持的字体。[UIFont familyName] 返回这些字体的列表“Chalkboard SE”
Thanks in advance!
提前致谢!
T
吨
回答by Steve HHH
[UIFont familyName]
is supported back to iPhone OS 2.0 (before 2.0, third-party apps were not allowed on iPhone or iPod touch) , so I would use that to check to see if a font family exists on the current device, and if it doesn't exist, use a suitable fall-back font for that version of iOS. Here's John Gruber's list of iPhone fonts from the original iPhone in 2007 (contrasted with the fonts in Mac OS X of the day). I haven't checked them all, but the iOS fonts I did check are still in iOS 5:
[UIFont familyName]
支持回 iPhone OS 2.0(在 2.0 之前,iPhone 或 iPod touch 上不允许使用第三方应用程序),所以我会用它来检查当前设备上是否存在字体系列,如果不存在存在,请为该版本的 iOS 使用合适的后备字体。这是 John Gruber 的 2007 年原始 iPhone 的 iPhone 字体列表(与当时 Mac OS X 中的字体形成对比)。我还没有全部检查过,但我检查过的 iOS 字体仍在 iOS 5 中:
Here's an example of using [UIFont familyName]
:
这是使用的示例[UIFont familyName]
:
NSLog (@"Font families: %@", [UIFont familyNames]);
This will produce a list like this:
这将产生一个这样的列表:
Font families: ( Thonburi, "Snell Roundhand", "Academy Engraved LET", ... et cetera.
字体系列:( Thonburi、“Snell Roundhand”、“Academy Engraved LET”等。
Once you know the family name, you can use the UIFont
class method fontNamesForFamilyName
to get an NSArray of the names of all the fonts in the family. For example:
一旦知道了系列名称,就可以使用UIFont
类方法fontNamesForFamilyName
来获取系列中所有字体名称的 NSArray。例如:
NSLog (@"Courier New family fonts: %@", [UIFont fontNamesForFamilyName:@"Courier New"]);
That will produce a list like this:
这将产生一个这样的列表:
Courier New family fonts: ( CourierNewPSMT, "CourierNewPS-BoldMT", "CourierNewPS-BoldItalicMT", "CourierNewPS-ItalicMT" )
Courier New 系列字体:(CourierNewPSMT、“CourierNewPS-BoldMT”、“CourierNewPS-BoldItalicMT”、“CourierNewPS-ItalicMT”)
The following example code prints a list of each font in every family on the current device:
以下示例代码打印当前设备上每个系列中每种字体的列表:
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (@"%@: %@", fontFamily, fontNames);
}
For more information, check out the iOS Developer Reference document for the UIFont
class methods familyNamesand fontNamesForFamilyName:.
有关更多信息,请查看UIFont
类方法familyNames和fontNamesForFamilyName:的 iOS 开发人员参考文档。
回答by Alessandro Pirovano
If you use Swift you can print all fonts (see below). You can also check if the font exists.
如果您使用 Swift,则可以打印所有字体(见下文)。您还可以检查字体是否存在。
for family in UIFont.familyNames {
print("\(family)")
for name in UIFont.fontNames(forFamilyName: family) {
print(" \(name)")
}
}
回答by matt
This font was not available in 4.1 but it is supported in 4.3. What would be the best way to go about checking if the font exists
此字体在 4.1 中不可用,但在 4.3 中受支持。检查字体是否存在的最佳方法是什么
Simply ask for the font in the usual way using UIFont* f = [UIFont fontWithName:... size:...];
. If the font isn't available, the result (f
) will be nil.
只需使用UIFont* f = [UIFont fontWithName:... size:...];
. 如果字体不可用,则结果 ( f
) 将为 nil。
(One way to guarantee the availability of a font is to include it in the app bundle...)
(保证字体可用性的一种方法是将其包含在应用程序包中...)
回答by Beau Nouvelle
Swift 5.x
斯威夫特 5.x
Forget those for-loops, this is the easiest way to see fonts supported by iOS.
忘记那些 for 循环,这是查看 iOS 支持的字体的最简单方法。
Just run any of the following in a playground.
只需在操场上运行以下任何一项。
Family Names Only
仅姓氏
Input
输入
dump(UIFont.familyNames)
Output
输出
? 75 elements
- "Copperplate"
- "Heiti SC"
- "Kohinoor Telugu"
...
Font Names Only
仅字体名称
Input
输入
dump(UIFont.familyNames.compactMap { UIFont.fontNames(forFamilyName: ? 248 elements
- "Copperplate-Light"
- "Copperplate"
- "Copperplate-Bold"
...
) })
Output
输出
dump(UIFont.fontNames(forFamilyName: "Helvetica Neue"))
Font Names for Specified Family Name
指定姓氏的字体名称
Input
输入
? 14 elements
- "HelveticaNeue-Italic"
- "HelveticaNeue-Bold"
- "HelveticaNeue-UltraLight"
- "HelveticaNeue-CondensedBlack"
...
Output
输出
NSFont *font = [NSFont fontWithName:@"thefont" size:25.0];
if (font==nil) {
// thefont is available
} else {
// thefont is not available
}
回答by disaster code
This is what i did on objective c to find out if font is available or not
这就是我在目标 c 上所做的,以确定字体是否可用
extension UIFont {
static func availableFonts() {
// Get all fonts families
for family in UIFont.familyNames() {
NSLog("\(family)")
// Show all fonts for any given family
for name in UIFont.fontNamesForFamilyName(family) {
NSLog(" \(name)")
}
}
}
}
回答by Jiri Trecak
For iOS9 / Swift 2.0, none of above won't work as the syntax changed a little. I also personally prefer to use extension (I choose to create one for UIFont, as it fits the best and modified @API answer as this was the best one):
对于 iOS9 / Swift 2.0,由于语法稍有变化,以上都不起作用。我个人也更喜欢使用扩展(我选择为 UIFont 创建一个,因为它适合最好的和修改过的 @API 答案,因为这是最好的一个):
UIFont.availableFonts()
Now you can just call:
现在你可以调用:
: Bangla Sangam MN
: BanglaSangamMN-Bold
: BanglaSangamMN
: Zapfino
: Zapfino
And it will tell you all the fonts in the form of
它会以以下形式告诉您所有字体
UIFont.familyNames().sort( { var fontFamilies = UIFont.familyNames()
for (var i:Int = 0; i < fontFamilies.count; i++) {
var fontFamily: NSString = fontFamilies[i] as NSString
var fontNames: NSArray = UIFont.fontNamesForFamilyName(fontFamilies[i] as String) as NSArray
NSLog ("%@: %@", fontFamily, fontNames)
}
< } ).forEach({ print("\(##代码##)"); UIFont.fontNamesForFamilyName("\(##代码##)").sort( { ##代码## < } ).forEach({ print(" \(##代码##)") }) })
Hope it helps!
希望能帮助到你!
回答by eMdOS
Swift version:
迅捷版:
##代码##回答by Pontus
Here is a conversion of Steves answer to Swift code (for quick copy and paste purpose):
这是 Steves 对 Swift 代码的回答的转换(用于快速复制和粘贴目的):
##代码##回答by superarts.org
Try to init with that font name, and if it's nil
do something else. Swift code:
尝试使用该字体名称进行初始化,如果它是nil
做其他事情。SWIFT代码:
if let font = UIFont(name: name, size: size) { // ok } else { // not ok }
if let font = UIFont(name: name, size: size) { // ok } else { // not ok }
回答by Sauvik Dolui
Well, rather than writing a single line of code, you can just open http://iosfonts.comand check availability based on your iOS version support. You can also know how would it look like.
好吧,您无需编写一行代码,只需打开http://iosfonts.com并根据您的 iOS 版本支持检查可用性。你也可以知道它会是什么样子。