将自定义字体添加到 iOS 应用程序以查找其真实姓名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15984937/
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
Adding custom fonts to iOS app finding their real names
提问by Muhammad Umar
I have two fonts to add in my app for using.
我有两种字体要添加到我的应用程序中以供使用。
Here is the font images. Currently the files are named as
这是字体图片。目前这些文件被命名为
name.font = [UIFont fontWithName:@"Helvetica Neue LT Pro-Medium" size:10];
headline.font = [UIFont fontWithName:@"Helvetica Neue LT Pro-Light" size:8];
putting same name in the Font Avaliable option in plist file
.
将相同的名称放在Font Avaliable option in plist file
.
I have also tried adding file names like
我也试过添加文件名,如
HelveticaNeueLTPro-Lt
HelveticaNeueLTPro-Md
but nothing seems to work. How can i get the exact name of the fonts.
但似乎没有任何效果。我怎样才能获得字体的确切名称。
回答by rob mayoff
Use +[UIFont familyNames]
to list all of the font family names known to the system. For each family name, you can then use +[UIFont fontNamesForFamilyName:]
to list all of the font names known to the system. Try printing those out to see what name the system expects. Example code:
使用+[UIFont familyNames]
列出所有已知的系统字体系列名称。对于每个系列名称,您可以使用+[UIFont fontNamesForFamilyName:]
来列出系统已知的所有字体名称。尝试将它们打印出来以查看系统期望的名称。示例代码:
static void dumpAllFonts() {
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(@"%@", fontName);
}
}
}
Put that in your app, call it, and see what you get. If you see a name in the output that looks appropriate for your font, use it. Otherwise, perhaps you haven't properly added the font to your app.
把它放在你的应用程序中,调用它,看看你得到了什么。如果您在输出中看到适合您字体的名称,请使用它。否则,也许您没有正确地将字体添加到您的应用程序中。
In swift 4.2+ use code given below:
在 swift 4.2+ 中使用下面给出的代码:
func dumpAllFonts() {
for familyName in UIFont.familyNames {
for fontName in UIFont.fontNames(forFamilyName: familyName) {
print("\(fontName)")
}
}
}
回答by iPatel
This is Step for, How to add custom font in Application.
这是步骤,如何在应用程序中添加自定义字体。
1 - Add .TTF
font in your application
2 - Modify the application-info.plist file
.
3 - Add the key "Fonts provided by application"to a new row
4 - and add each .TTF
file (of font) to each line.
1 -.TTF
在您的应用程序中添加字体
2 - 修改application-info.plist file
.
3 - 将键“应用程序提供的字体”添加到新行
4 - 并将每个.TTF
文件(字体)添加到每一行。
Also read Thisand Thistutorials for improve your knowledge.
FOR MOREINFORMATION :
想要查询更多的信息 :
But after adding font to your application, sometimes might be the font name is not the name of the file name so you need to apply/writereal name of your font, so check @NSGod'sanswer.
但是在将字体添加到您的应用程序后,有时可能是字体名称不是文件名的名称,因此您需要应用/写入字体的真实名称,因此请查看 @NSGod 的答案。
Following is code for find all fonts from the system:
以下是从系统中查找所有字体的代码:
for(NSString *fontfamilyname in [UIFont familyNames])
{
NSLog(@"Family:'%@'",fontfamilyname);
for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname])
{
NSLog(@"\tfont:'%@'",fontName);
}
NSLog(@"~~~~~~~~");
}
Might be above step and suggestion is helpful in your case:
可能高于步骤,建议对您的情况有帮助:
回答by NSGod
While it appears that UIFont
's fontNamed:size:
method is fairly forgiving with the names you provide, the official fontName
has always been the font's internal PostScript name. A font's PostScript name cannot contain spaces and usually has them replaced by a hyphen. I don't recall offhand, but I believe they may also be limited to 31 characters.
虽然UIFont
'sfontNamed:size:
方法似乎对您提供的名称相当宽容,但官方fontName
一直是字体的内部 PostScript 名称。字体的 PostScript 名称不能包含空格,通常用连字符替换它们。我不记得了,但我相信它们也可能被限制为 31 个字符。
Determining what name you should use in code is extremely simple. Double click on the font you want to use on your iOS device to open it in Font Book. If the font is not already installed, click the Install button in the sample window that appears. When it's installed, select the font in the Font list. In Font Book's menu, choose Preview > Show Font Info. That will show info about the font like shown in the image below:
确定您应该在代码中使用什么名称非常简单。双击要在 iOS 设备上使用的字体以在 Font Book 中打开它。如果尚未安装字体,请在出现的示例窗口中单击安装按钮。安装后,在字体列表中选择字体。在 Font Book 的菜单中,选择 Preview > Show Font Info。这将显示有关字体的信息,如下图所示:
As you can see, the PostScript name for Helvetica Neue LT Std 65 Medium
is HelveticaNeueLTStd-Md
. That's the name you should use in code.
如您所见,PostScript 名称Helvetica Neue LT Std 65 Medium
是HelveticaNeueLTStd-Md
. 这是您应该在代码中使用的名称。
In your Info.plist
file, under fonts for the application, you need to use the actual filename of the font itself, whatever that happens to be. In my case, it was HelveticaNeueLTStd-Md.otf
.
在您的Info.plist
文件中,在应用程序的字体下,您需要使用字体本身的实际文件名,无论它是什么。就我而言,它是HelveticaNeueLTStd-Md.otf
.
回答by andreagiavatto
Easier way to get the name of the font
获取字体名称的更简单方法
- Right-click on your .ttf/.otf file and choose "Get Info"
- Read the
Full Name
property and use that as the name of the font
- 右键单击您的 .ttf/.otf 文件并选择“获取信息”
- 读取
Full Name
属性并将其用作字体名称
It works both in the info .plist file (adding the extension) and in the code with [UIFont fontWithName:<Full Name> size:17]
它适用于 info .plist 文件(添加扩展名)和代码中[UIFont fontWithName:<Full Name> size:17]
回答by Nazir
Just NSLog The font you want get:
只要 NSLog 想要得到的字体:
NSLog(@" %@", [UIFont fontNamesForFamilyName:@"American Typewriter"]);
NSLog(@" %@", [UIFont fontNamesForFamilyName:@"American Typewriter"]);
And you get the array:
你得到数组:
(
"AmericanTypewriter-CondensedLight",
"AmericanTypewriter-Light",
"AmericanTypewriter-Bold",
AmericanTypewriter,
"AmericanTypewriter-CondensedBold",
"AmericanTypewriter-Condensed"
)
Use any you need in Code any of font names AmericanTypewriter-Bold
as [UIFont fontWithName:@"AmericanTypewriter-Bold" size:12.0]
在代码中使用任何您需要的任何字体名称AmericanTypewriter-Bold
作为[UIFont fontWithName:@"AmericanTypewriter-Bold" size:12.0]
Also you could get All Applied Fonts grouped by Font Family
您也可以获得按字体系列分组的所有应用字体
for (NSString* family in [UIFont familyNames])
{
NSLog(@"%@", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(@" %@", name);
}
}
回答by nahung89
I write some code to print out only font name that you want, not just print out all fonts.
我编写了一些代码来仅打印出您想要的字体名称,而不仅仅是打印出所有字体。
- (NSString *)realNameOfFont:(NSString *)fullFontName {
NSString *fontName = [[fullFontName componentsSeparatedByString:@"."] firstObject];
NSString *ext = [fullFontName pathExtension];
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fontName ofType:ext]];
if (data == nil) {
NSLog(@"Failed to load font. Data at path %@ is null", fullFontName);
return nil;
}
CFErrorRef errorRef;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
CGFontRef font = CGFontCreateWithDataProvider(provider);
NSString *realFontName = (__bridge NSString *)CGFontCopyPostScriptName(font);
if (!CTFontManagerRegisterGraphicsFont(font, &errorRef)) {
NSError *error = (__bridge NSError *)errorRef;
if (error.code != kCTFontManagerErrorAlreadyRegistered) {
NSLog(@"Failed to load font: %@", error);
}
}
NSLog(@"Real font name: %@", realFontName);
CFRelease(font);
CFRelease(provider);
return realFontName;
}
Example:
例子:
NSArray *fonts = @[@"bg_en_AmaticSC-Bold111.otf", @"bg_en_segoescb_0.ttf", @"bg_en_UTM Cookies_2.ttf", @"bg_en_VSCOProximaExtrabold_0.otf"];
for (NSString *fontFullName in fonts) {
[self realNameOfFont:fontFullName];
}
Result:
结果:
Failed to load font. Data at path bg_en_AmaticSC-Bold111.otf is null
Real font name: SegoeScript-Bold
Real font name: UTM-Cookies
Real font name: ProximaNova-Extrabld
回答by Tommie C.
Nothing wrong with the other answers. In fact iPatel's prerequisites continue to apply.
其他答案没有任何问题。事实上,iPatel 的先决条件继续适用。
In Swift 2.2+, the call to show the family names is simply:
在 Swift 2.2+ 中,显示姓氏的调用很简单:
UIFont.familyNames().sort().forEach({ (familyName) in
debugPrint(UIFont.fontNamesForFamilyName(familyName))
})
If you have installed the fonts on your system, you could also simply add the fonts to your UI element in the storyboard.
如果您已经在系统上安装了字体,您还可以简单地将字体添加到故事板中的 UI 元素中。
Note
笔记
However, I have noticed a bug in some cases where after one add the fonts properly to the project, info.plist, and UI elements in the storyboard, they still do not properly reflect your desired changes. In some cases toggling the settings will help the Storyboard sync it's underlying xml. In other cases you might need to simply update the size of your custom font in code to have the changes take effect. The simplest method in Swift that I have found is:
但是,我注意到在某些情况下存在一个错误,即在将字体正确添加到故事板中的项目、info.plist 和 UI 元素后,它们仍然无法正确反映您想要的更改。在某些情况下,切换设置将有助于 Storyboard 同步它的底层 xml。在其他情况下,您可能需要简单地更新代码中自定义字体的大小才能使更改生效。我发现的 Swift 中最简单的方法是:
@IBOutlet weak var startMessage: UILabel!
startMessage.font = UIFont(name: startMessage.font.familyName, size: 31.0)
Which assumes the custom font is already set in the Storyboard and simply updates the size in code. This workaround would ensure that if all other prerequisites are met that you actually see the correct custom typeface. It also removes the need to actually know the underlying font name (although if desired the initial Swift code above will give you the name).
假设自定义字体已经在 Storyboard 中设置,并且只需在代码中更新大小。此解决方法将确保如果满足所有其他先决条件,您实际上会看到正确的自定义字体。它还消除了实际知道底层字体名称的需要(尽管如果需要,上面的初始 Swift 代码将为您提供名称)。
回答by Esqarrouth
Swift version:
迅捷版:
static func printAllFontNames() {
for familyname in UIFont.familyNames() {
for fname in UIFont.fontNamesForFamilyName(familyname) {
print(fname)
}
}
}
回答by daidai
Swift 3 version:
斯威夫特 3 版本:
UIFont.familyNames.sorted().forEach({ (familyName) in
print(UIFont.fontNames(forFamilyName: familyName))
})
回答by Josep Escobar
Update for swift 3
更新 swift 3
for familyname in UIFont.familyNames {
for fname in UIFont.fontNames(forFamilyName: familyname) {
print(fname)
}
}