使用自定义字体

发布于 2024-07-28 18:23:14 字数 895 浏览 1 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人问我粥可暖 2024-08-04 18:25:12

这些将在您的应用程序上加载所有“ttf”字体,并可供

[UIFont fontWithName:@"FontFamilyName" size:20];

XXXX.m加载

#import <dlfcn.h>

-(void)viewDidLoad
{ 
[self loadFonts];
}

-(void)loadFonts{
 NSLog(@" Fonts  NO : %d",[[UIFont familyNames] count]);
 NSlog(@"Start Load Fonts");
NSUInteger loadFonts();{
    NSUInteger newFontCount = 0;
    NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.apple.GraphicsServices"];
    const char *frameworkPath = [[frameworkBundle executablePath] UTF8String];
    if (frameworkPath) {
        void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY);
        if (graphicsServices) {
            BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, "GSFontAddFromFile");
            if (GSFontAddFromFile)
                for (NSString *fontFile in [[NSBundle mainBundle]   pathsForResourcesOfType:@"ttf" inDirectory:nil])
newFontCount += GSFontAddFromFile([fontFile UTF8String]);
NSLog(@" Fonts  NO : %d",[[UIFont familyNames] count]);  
NSLog(@" Font Family Names : %@",[UIFont familyNames] );             
NSlog(@"End Load Fonts");
}}
//  return newFontCount;
}}

these will load all "ttf" fonts on you App and make available to lode by

[UIFont fontWithName:@"FontFamilyName" size:20];

XXXX.m

#import <dlfcn.h>

-(void)viewDidLoad
{ 
[self loadFonts];
}

-(void)loadFonts{
 NSLog(@" Fonts  NO : %d",[[UIFont familyNames] count]);
 NSlog(@"Start Load Fonts");
NSUInteger loadFonts();{
    NSUInteger newFontCount = 0;
    NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.apple.GraphicsServices"];
    const char *frameworkPath = [[frameworkBundle executablePath] UTF8String];
    if (frameworkPath) {
        void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY);
        if (graphicsServices) {
            BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, "GSFontAddFromFile");
            if (GSFontAddFromFile)
                for (NSString *fontFile in [[NSBundle mainBundle]   pathsForResourcesOfType:@"ttf" inDirectory:nil])
newFontCount += GSFontAddFromFile([fontFile UTF8String]);
NSLog(@" Fonts  NO : %d",[[UIFont familyNames] count]);  
NSLog(@" Font Family Names : %@",[UIFont familyNames] );             
NSlog(@"End Load Fonts");
}}
//  return newFontCount;
}}
晨与橙与城 2024-08-04 18:24:45

所以我假设字体的文件名是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.

第几種人 2024-08-04 18:24:16

已经有一段时间了,我相信您已经想出了解决方案。 现在有一个很好、干净的方法:

将 .ttf 或 .otf 文件保存在项目目录中并导入到 Xcode。

在 .Plist 中添加字段 UIAppFonts (数组)或选择选项“应用程序提供的字体”并将文件名添加到第一个条目。

在 .h 中创建 UIFont 实例:

UIFont *myFont;

...

@property (nonatomic, retain) UIFont *myFont;

然后在 .m 中合成它: @ Synthesize myFont

在某些方法或操作中,调用 NSLog(@"%@", [UIFont familyNames]) ;

在列表中找到您的新字体,并准确记下它显示的名称

在您的 ViewDidLoad() 方法中或您想要的任何位置,

myFont = [UIFont fontWithName:@"FontNameFromPreviousStep" size:20];

使用您的方法更改标签的字体或其他内容: 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:

UIFont *myFont;

...

@property (nonatomic, retain) UIFont *myFont;

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

myFont = [UIFont fontWithName:@"FontNameFromPreviousStep" size:20];

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文