使用自定义字体
我想在 iPhone 应用程序中使用自定义字体。 我已经有两个文件“字体手提箱”及其支持的文件“postscript type 1”。 我这样使用:
NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"Myfont" ofType:nil];
if( fontPath == nil )
{
return;
}
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename( [ fontPath UTF8String]);
if( fontDataProvider == nil )
{
return;
}
// Create the font with the data provider, then release the data provider.
customFont = CGFontCreateWithDataProvider(fontDataProvider);
if( customFont != nil )
{
bLoadedFontFile = YES;
}
CGDataProviderRelease(fontDataProvider);
...
“Myfont”是一个 postscript 文件。 CGDataProviderCreateWithFilename
API 返回NULL
。
谢谢 维兰德拉 电子邮件 ID:[电子邮件受保护]
I want to use custom font in an iPhone application. I have already two file 'font suitcase' and its supported file 'postscript type 1 '. I am using like this:
NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"Myfont" ofType:nil];
if( fontPath == nil )
{
return;
}
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename( [ fontPath UTF8String]);
if( fontDataProvider == nil )
{
return;
}
// Create the font with the data provider, then release the data provider.
customFont = CGFontCreateWithDataProvider(fontDataProvider);
if( customFont != nil )
{
bLoadedFontFile = YES;
}
CGDataProviderRelease(fontDataProvider);
...
'Myfont' is a postscript file. CGDataProviderCreateWithFilename
API returns NULL
.
Thanks
Virendra
E-Mail ID: [email protected]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些将在您的应用程序上加载所有“ttf”字体,并可供
XXXX.m加载
these will load all "ttf" fonts on you App and make available to lode by
XXXX.m
所以我假设字体的文件名是MyFont.? 如果是这样,我相信您可能需要在对 pathForResource 的调用中设置类型或在资源名称后附加后缀。
顺便说一句,我使用了 this SO 中的信息问题在iPhone上成功渲染自定义字体。 然而,有问题的字体是 True Type 格式。
So I assume that the file name of the font is MyFont.<some-extention>? If so I believe that you may need to set the type in the call to pathForResource or append a suffix to the resource name.
As an aside, I have used the information in this SO question to render custom fonts on the iPhone with success. However, the fonts in question were in the True Type format.
已经有一段时间了,我相信您已经想出了解决方案。 现在有一个很好、干净的方法:
将 .ttf 或 .otf 文件保存在项目目录中并导入到 Xcode。
在 .Plist 中添加字段 UIAppFonts (数组)或选择选项“应用程序提供的字体”并将文件名添加到第一个条目。
在 .h 中创建 UIFont 实例:
然后在 .m 中合成它:
@ Synthesize myFont
在某些方法或操作中,调用
NSLog(@"%@", [UIFont familyNames]) ;
在列表中找到您的新字体,并准确记下它显示的名称
在您的 ViewDidLoad() 方法中或您想要的任何位置,
使用您的方法更改标签的字体或其他内容:
label .font = myFont;
清理 NSLog(),使其不使用内存资源
It's been a while now, and I'm sure you've come up with your solution. Here's a nice, clean way to do it now:
Save your .ttf or .otf file in your project directory and import to Xcode.
In .Plist add field UIAppFonts (an array) or choose option "Fonts provided by application" and add the file name to the first entry.
Create an instance of UIFont in your .h:
then synthesize it in .m:
@ synthesize myFont
In some method or action, call
NSLog(@"%@", [UIFont familyNames]);
Find your new font in the list and take down the name exactly as it appears
In your ViewDidLoad() method or wherever you want, code
Use your methods to change the font of the label or whatever:
label.font = myFont;
Clean up the NSLog() so it doesn't use memory resources