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"
我目前正在开发一个应用程序,它使用“chalkboard - regular”字体,我的部署目标是4.0+。4.1中没有这种字体,但是4.3中支持这种字体。检查字体是否存在,如果不存在,最好的方法是在iOS的<4.1版本上使用另一种受支持的字体。返回这些字体的列表“黑板SE”
Thanks in advance!
提前谢谢!
T
T
10 个解决方案
#1
102
[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字体列表(与当时Mac OS X中的字体形成对比)。我还没有全部检查过,但是我检查过的iOS字体仍然在ios5中:
- http://daringfireball.net/misc/2007/07/iphone-osx-fonts
- http://daringfireball.net/misc/2007/07/iphone-osx-fonts
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 engravlet”,…等等。
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" )
信使字体:(CourierNewPSMT,“CourierNewPS-BoldMT”,“CourierNewPS-BoldMT - 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 familyNames and fontNamesForFamilyName:.
要了解更多信息,请查看iuifont类方法familyNames和fontNamesForFamilyName:的iOS开发人员参考文档。
#2
29
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)")
}
}
#3
14
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:…尺寸:…]。如果字体不可用,则结果(f)将为nil。
(One way to guarantee the availability of a font is to include it in the app bundle...)
(保证字体可用的一种方法是将它包含在应用程序包中……)
#4
3
Swift 3.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.flatMap { UIFont.fontNames(forFamilyName: $0) })
Output
输出
▿ 248 elements
- "Copperplate-Light"
- "Copperplate"
- "Copperplate-Bold"
...
Font Names for Specified Family Name
Input
输入
dump(UIFont.fontNames(forFamilyName: "Helvetica Neue"))
Output
输出
▿ 14 elements
- "HelveticaNeue-Italic"
- "HelveticaNeue-Bold"
- "HelveticaNeue-UltraLight"
- "HelveticaNeue-CondensedBlack"
...
#5
2
This is what i did on objective c to find out if font is available or not
这是我在objective c中做的,以确定字体是否可用
NSFont *font = [NSFont fontWithName:@"thefont" size:25.0];
if (font==nil) {
// thefont is available
} else {
// thefont is not available
}
#6
2
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答案,因为这是最好的答案):
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)")
}
}
}
}
Now you can just call:
现在你可以打:
UIFont.availableFonts()
And it will tell you all the fonts in the form of
它会告诉你所有字体的形式。
: Bangla Sangam MN
: BanglaSangamMN-Bold
: BanglaSangamMN
: Zapfino
: Zapfino
Hope it helps!
希望它可以帮助!
#7
2
Swift version:
斯威夫特版本:
UIFont.familyNames().sort( { $0 < $1 } ).forEach({ print("\($0)"); UIFont.fontNamesForFamilyName("\($0)").sort( { $0 < $1 } ).forEach({ print(" \($0)") }) })
#8
0
Here is a conversion of Steves answer to Swift code (for quick copy and paste purpose):
以下是Steves对Swift代码的转换(用于快速复制和粘贴):
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)
}
#9
0
Try to init with that font name, and if it's nil
do something else. Swift code:
尝试用那个字体名初始化,如果它是nil,做点别的。斯威夫特代码:
if let font = UIFont(name: name, size: size) { // ok } else { // not ok }
如果让字体= UIFont(name: name, size: size) {// / ok} else {// not ok}
#10
0
For objective-c
为objective - c
for (NSString *family in UIFont.familyNames) {
NSLog(@"family %@",family);
for (NSString *name in [UIFont fontNamesForFamilyName:family]) {
NSLog(@" name = %@",name);
}
}
#1
102
[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字体列表(与当时Mac OS X中的字体形成对比)。我还没有全部检查过,但是我检查过的iOS字体仍然在ios5中:
- http://daringfireball.net/misc/2007/07/iphone-osx-fonts
- http://daringfireball.net/misc/2007/07/iphone-osx-fonts
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 engravlet”,…等等。
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" )
信使字体:(CourierNewPSMT,“CourierNewPS-BoldMT”,“CourierNewPS-BoldMT - 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 familyNames and fontNamesForFamilyName:.
要了解更多信息,请查看iuifont类方法familyNames和fontNamesForFamilyName:的iOS开发人员参考文档。
#2
29
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)")
}
}
#3
14
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:…尺寸:…]。如果字体不可用,则结果(f)将为nil。
(One way to guarantee the availability of a font is to include it in the app bundle...)
(保证字体可用的一种方法是将它包含在应用程序包中……)
#4
3
Swift 3.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.flatMap { UIFont.fontNames(forFamilyName: $0) })
Output
输出
▿ 248 elements
- "Copperplate-Light"
- "Copperplate"
- "Copperplate-Bold"
...
Font Names for Specified Family Name
Input
输入
dump(UIFont.fontNames(forFamilyName: "Helvetica Neue"))
Output
输出
▿ 14 elements
- "HelveticaNeue-Italic"
- "HelveticaNeue-Bold"
- "HelveticaNeue-UltraLight"
- "HelveticaNeue-CondensedBlack"
...
#5
2
This is what i did on objective c to find out if font is available or not
这是我在objective c中做的,以确定字体是否可用
NSFont *font = [NSFont fontWithName:@"thefont" size:25.0];
if (font==nil) {
// thefont is available
} else {
// thefont is not available
}
#6
2
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答案,因为这是最好的答案):
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)")
}
}
}
}
Now you can just call:
现在你可以打:
UIFont.availableFonts()
And it will tell you all the fonts in the form of
它会告诉你所有字体的形式。
: Bangla Sangam MN
: BanglaSangamMN-Bold
: BanglaSangamMN
: Zapfino
: Zapfino
Hope it helps!
希望它可以帮助!
#7
2
Swift version:
斯威夫特版本:
UIFont.familyNames().sort( { $0 < $1 } ).forEach({ print("\($0)"); UIFont.fontNamesForFamilyName("\($0)").sort( { $0 < $1 } ).forEach({ print(" \($0)") }) })
#8
0
Here is a conversion of Steves answer to Swift code (for quick copy and paste purpose):
以下是Steves对Swift代码的转换(用于快速复制和粘贴):
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)
}
#9
0
Try to init with that font name, and if it's nil
do something else. Swift code:
尝试用那个字体名初始化,如果它是nil,做点别的。斯威夫特代码:
if let font = UIFont(name: name, size: size) { // ok } else { // not ok }
如果让字体= UIFont(name: name, size: size) {// / ok} else {// not ok}
#10
0
For objective-c
为objective - c
for (NSString *family in UIFont.familyNames) {
NSLog(@"family %@",family);
for (NSString *name in [UIFont fontNamesForFamilyName:family]) {
NSLog(@" name = %@",name);
}
}