It looks like there a few working solutions for using custom true type fonts (ttf) on the iPhone, most notably Can I embed a custom font in an iPhone application? However, this method does not support open type fonts (otf).
看起来有一些工作解决方案在iPhone上使用自定义真实字体(ttf),最值得注意的是我可以在iPhone应用程序中嵌入自定义字体吗?但是,此方法不支持开放式字体(otf)。
Has anyone come across a way to make use of otf typefaces?
有没有人遇到过使用otf字体的方法?
6 个解决方案
#1
22
Must you use an otf font on the device or can you convert it to ttf? It is relatively straightforward to convert an OTF font to a TTF font using a tool such as fontforge, perhaps that would be an easy solution.
您必须在设备上使用otf字体,还是可以将其转换为ttf?使用诸如fontforge之类的工具将OTF字体转换为TTF字体相对简单,这可能是一个简单的解决方案。
#2
70
For my own project I wanted to use the opentype font and I basically used this method:
对于我自己的项目,我想使用opentype字体,我基本上使用这个方法:
Add your custom font file into your project using XCode as a resource Add a key to your info.plist file (Fonts provided by application) For each font you have, enter the full name of your font file (including the extension) save info.plist then use this for your labels (ex:)
使用XCode作为资源将自定义字体文件添加到项目中将信息添加到info.plist文件中(应用程序提供的字体)对于您拥有的每种字体,请输入字体文件的全名(包括扩展名)保存信息。 plist然后用它来做你的标签(ex :)
UIFont* font = [UIFont fontWithName:@"Harrowprint" size:20];
In my case @"Harrowprint"
was the filename without the extension and not the font name provided by Finder.
在我的情况下,@“Harrowprint”是没有扩展名的文件名,而不是Finder提供的字体名称。
My font was an otf and it worked...so I guess it's supported...or I was lucky. I'll test another font..and keep you posted..
我的字体是otf并且它有效...所以我猜它得到了支持......或者我很幸运。我会测试另一种字体..并让你发布..
references:
引用:
- http://blog.beefyapps.com/2010/06/custom-fonts-in-ios-4/
- http://blog.beefyapps.com/2010/06/custom-fonts-in-ios-4/
- Can I embed a custom font in an iPhone application?
- 我可以在iPhone应用程序中嵌入自定义字体吗?
#3
17
I had trouble with this, too. Here is what worked for me:
我也遇到了麻烦。这对我有用:
- Copy the font files into the Resources.
- 将字体文件复制到资源中。
- Check to see that the font files are in 'Build Phases'->'Copy Bundle Resources' (Xcode put them there for me when I added the files to the Resources. If, for some reason, they are not there, add them.
- 检查字体文件是否在“构建阶段” - >“复制捆绑资源”(当我将文件添加到资源时,Xcode将它们放在那里。如果由于某种原因,它们不存在,请添加它们。
- Add the filenames to your info.plist file as describe above. Include the extensions.
- 如上所述,将文件名添加到info.plist文件中。包括扩展名。
- Open Font Book, select your font and Preview-Show Font Info and look at the 'Family', e.g., 'Foo'
- 打开字体书,选择字体和预览 - 显示字体信息,然后查看“家庭”,例如“Foo”
- Put this in your code somewhere and set a breakpoint to check the array of names:
NSArray *names = [UIFont fontNamesForFamilyName:@"Foo";
- 把它放在你的代码中并设置一个断点来检查名字数组:NSArray * names = [UIFont fontNamesForFamilyName:@“Foo”;
- Run your app and when it breaks, right-click on the 'names' variable in the debugger. This will show you the font names to use, e.g.:
<__NSCFArray 0xe9c4da0>( Foo-100Italic, Foo-100 )
Note that my font names were not shown correctly anywhere else, not in the filename nor in Font Book. - 运行您的应用程序,当它崩溃时,右键单击调试器中的“名称”变量。这将显示要使用的字体名称,例如:<__ NSCFArray 0xe9c4da0>(Foo-100Italic,Foo-100)请注意,我的字体名称在其他任何地方都没有正确显示,不在文件名或字体簿中。
- You should be good to go with:
myLabel.font = [UIFont fontWithName:@"Foo-100Italic" size:24];
- 你应该很高兴:myLabel.font = [UIFont fontWithName:@“Foo-100Italic”size:24];
#4
7
This article answers your question (it worked like a charm for me using OTF): http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
这篇文章回答了你的问题(它对我来说就像使用OTF的魅力):http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
OTF is perfectly supported, no need to convert to TTF
完美支持OTF,无需转换为TTF
#5
3
With regards to the first answer - the solution is correct (i.e. just convert the font over to TTF) but the software suggested is a super hassle to compile and install. Instead I just did a quick google search and used this instead: http://www.freefontconverter.com/ and that worked perfectly.
关于第一个答案 - 解决方案是正确的(即只是将字体转换为TTF),但建议的软件编译和安装是一件非常麻烦的事情。相反,我只是做了一个快速的谷歌搜索,而是使用它:http://www.freefontconverter.com/,并且工作得很好。
(Make sure to add the font to your info.plist file as well)
(确保将字体添加到info.plist文件中)
#6
2
I researched this throughly, and this is the best method I found:
我彻底研究了这个,这是我找到的最好的方法:
1) Convert font file to TTF using this online tool: http://www.freefontconverter.com/
1)使用此在线工具将字体文件转换为TTF:http://www.freefontconverter.com/
2) Add the TTF files to your project
2)将TTF文件添加到项目中
3) Make some configurations to your info.plist file:
3)对info.plist文件进行一些配置:
3.1) Edit info.plist as source code (right click on file -> open as -> source code)
3.1)编辑info.plist作为源代码(右键单击文件 - >打开为 - >源代码)
3.2) Add this:
3.2)添加:
<key>UIAppFonts</key> <array> <string>font1.ttf</string> <string>font2.ttf</string> ...etc... </array>
3.3) Your info.plist should have this now:
3.3)你的info.plist现在应该有:
4) Use the new fonts just like the system fonts:
4)像系统字体一样使用新字体:
[UIFont fontWithName:@"font1" size:16.0];
[UIFont fontWithName:@“font1”size:16.0];
(Notice no ".ttf" in the font name)
(注意字体名称中没有“.ttf”)
#1
22
Must you use an otf font on the device or can you convert it to ttf? It is relatively straightforward to convert an OTF font to a TTF font using a tool such as fontforge, perhaps that would be an easy solution.
您必须在设备上使用otf字体,还是可以将其转换为ttf?使用诸如fontforge之类的工具将OTF字体转换为TTF字体相对简单,这可能是一个简单的解决方案。
#2
70
For my own project I wanted to use the opentype font and I basically used this method:
对于我自己的项目,我想使用opentype字体,我基本上使用这个方法:
Add your custom font file into your project using XCode as a resource Add a key to your info.plist file (Fonts provided by application) For each font you have, enter the full name of your font file (including the extension) save info.plist then use this for your labels (ex:)
使用XCode作为资源将自定义字体文件添加到项目中将信息添加到info.plist文件中(应用程序提供的字体)对于您拥有的每种字体,请输入字体文件的全名(包括扩展名)保存信息。 plist然后用它来做你的标签(ex :)
UIFont* font = [UIFont fontWithName:@"Harrowprint" size:20];
In my case @"Harrowprint"
was the filename without the extension and not the font name provided by Finder.
在我的情况下,@“Harrowprint”是没有扩展名的文件名,而不是Finder提供的字体名称。
My font was an otf and it worked...so I guess it's supported...or I was lucky. I'll test another font..and keep you posted..
我的字体是otf并且它有效...所以我猜它得到了支持......或者我很幸运。我会测试另一种字体..并让你发布..
references:
引用:
- http://blog.beefyapps.com/2010/06/custom-fonts-in-ios-4/
- http://blog.beefyapps.com/2010/06/custom-fonts-in-ios-4/
- Can I embed a custom font in an iPhone application?
- 我可以在iPhone应用程序中嵌入自定义字体吗?
#3
17
I had trouble with this, too. Here is what worked for me:
我也遇到了麻烦。这对我有用:
- Copy the font files into the Resources.
- 将字体文件复制到资源中。
- Check to see that the font files are in 'Build Phases'->'Copy Bundle Resources' (Xcode put them there for me when I added the files to the Resources. If, for some reason, they are not there, add them.
- 检查字体文件是否在“构建阶段” - >“复制捆绑资源”(当我将文件添加到资源时,Xcode将它们放在那里。如果由于某种原因,它们不存在,请添加它们。
- Add the filenames to your info.plist file as describe above. Include the extensions.
- 如上所述,将文件名添加到info.plist文件中。包括扩展名。
- Open Font Book, select your font and Preview-Show Font Info and look at the 'Family', e.g., 'Foo'
- 打开字体书,选择字体和预览 - 显示字体信息,然后查看“家庭”,例如“Foo”
- Put this in your code somewhere and set a breakpoint to check the array of names:
NSArray *names = [UIFont fontNamesForFamilyName:@"Foo";
- 把它放在你的代码中并设置一个断点来检查名字数组:NSArray * names = [UIFont fontNamesForFamilyName:@“Foo”;
- Run your app and when it breaks, right-click on the 'names' variable in the debugger. This will show you the font names to use, e.g.:
<__NSCFArray 0xe9c4da0>( Foo-100Italic, Foo-100 )
Note that my font names were not shown correctly anywhere else, not in the filename nor in Font Book. - 运行您的应用程序,当它崩溃时,右键单击调试器中的“名称”变量。这将显示要使用的字体名称,例如:<__ NSCFArray 0xe9c4da0>(Foo-100Italic,Foo-100)请注意,我的字体名称在其他任何地方都没有正确显示,不在文件名或字体簿中。
- You should be good to go with:
myLabel.font = [UIFont fontWithName:@"Foo-100Italic" size:24];
- 你应该很高兴:myLabel.font = [UIFont fontWithName:@“Foo-100Italic”size:24];
#4
7
This article answers your question (it worked like a charm for me using OTF): http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
这篇文章回答了你的问题(它对我来说就像使用OTF的魅力):http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
OTF is perfectly supported, no need to convert to TTF
完美支持OTF,无需转换为TTF
#5
3
With regards to the first answer - the solution is correct (i.e. just convert the font over to TTF) but the software suggested is a super hassle to compile and install. Instead I just did a quick google search and used this instead: http://www.freefontconverter.com/ and that worked perfectly.
关于第一个答案 - 解决方案是正确的(即只是将字体转换为TTF),但建议的软件编译和安装是一件非常麻烦的事情。相反,我只是做了一个快速的谷歌搜索,而是使用它:http://www.freefontconverter.com/,并且工作得很好。
(Make sure to add the font to your info.plist file as well)
(确保将字体添加到info.plist文件中)
#6
2
I researched this throughly, and this is the best method I found:
我彻底研究了这个,这是我找到的最好的方法:
1) Convert font file to TTF using this online tool: http://www.freefontconverter.com/
1)使用此在线工具将字体文件转换为TTF:http://www.freefontconverter.com/
2) Add the TTF files to your project
2)将TTF文件添加到项目中
3) Make some configurations to your info.plist file:
3)对info.plist文件进行一些配置:
3.1) Edit info.plist as source code (right click on file -> open as -> source code)
3.1)编辑info.plist作为源代码(右键单击文件 - >打开为 - >源代码)
3.2) Add this:
3.2)添加:
<key>UIAppFonts</key> <array> <string>font1.ttf</string> <string>font2.ttf</string> ...etc... </array>
3.3) Your info.plist should have this now:
3.3)你的info.plist现在应该有:
4) Use the new fonts just like the system fonts:
4)像系统字体一样使用新字体:
[UIFont fontWithName:@"font1" size:16.0];
[UIFont fontWithName:@“font1”size:16.0];
(Notice no ".ttf" in the font name)
(注意字体名称中没有“.ttf”)