I want to create an app that uses the user's custom font, therefore I want to my app to load the ttf file at run time. I am familiar with adding custom font via the UIAppFonts key as discussed in Adding custom font on iphone. Presumably I wouldn't be successful creating a placeholder custom font that could be swapped out by my app, if provided a user-created custom version?
我想创建一个使用用户自定义字体的应用程序,因此我想让我的应用程序在运行时加载ttf文件。我熟悉通过uiappfont键添加自定义字体,如在iphone上添加自定义字体所讨论的。假设我不能成功地创建一个占位符自定义字体,如果提供一个用户创建的自定义版本,我的应用程序可以将其替换掉?
1 个解决方案
#1
3
The font file I used was this: http://www.webpagepublicity.com/free-fonts/a/Almagro%20Regular.ttf
我使用的字体文件是:http://www.webpagepublicity.com/free-fonts/a/Almagro%20Regular.ttf
And I did it using this code:
我用的代码是
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontFilepath UTF8String]);
// Create the font with the data provider, then release the data provider.
CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
CFErrorRef error = nil;
CTFontManagerRegisterGraphicsFont(customFont, &error);
CGFontRelease(customFont);
if (error != nil)
{
NSError* err = (__bridge NSError*)error;
NSLog(@"error code %d desc: %@",err.code, [err description]);
}
UIFont* f = [UIFont fontWithName:@"Almagro" size:14.0];
There are some gotcha's though. You need to know the font name since it might differ slightly from the file name. In my example, the file name was "Almagro Regular.ttf" but the font name was just "Almagro". On another forum someone said that you need to be careful about fonts with spaces in their names because a font like "Sans Serif" would be registered as "SansSerif" for UIFont fontWithName:size:
不过还是有一些问题。您需要知道字体名称,因为它可能与文件名稍有不同。在我的示例中,文件名是“Almagro Regular”。ttf,但是字体名称只是“Almagro”。在另一个论坛上,有人说,你需要注意名称中有空格的字体,因为像“SansSerif”这样的字体会被注册为“SansSerif”,用于UIFont fontWithName:size:
Let me know if it works.
如果有用,请告诉我。
#1
3
The font file I used was this: http://www.webpagepublicity.com/free-fonts/a/Almagro%20Regular.ttf
我使用的字体文件是:http://www.webpagepublicity.com/free-fonts/a/Almagro%20Regular.ttf
And I did it using this code:
我用的代码是
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontFilepath UTF8String]);
// Create the font with the data provider, then release the data provider.
CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
CFErrorRef error = nil;
CTFontManagerRegisterGraphicsFont(customFont, &error);
CGFontRelease(customFont);
if (error != nil)
{
NSError* err = (__bridge NSError*)error;
NSLog(@"error code %d desc: %@",err.code, [err description]);
}
UIFont* f = [UIFont fontWithName:@"Almagro" size:14.0];
There are some gotcha's though. You need to know the font name since it might differ slightly from the file name. In my example, the file name was "Almagro Regular.ttf" but the font name was just "Almagro". On another forum someone said that you need to be careful about fonts with spaces in their names because a font like "Sans Serif" would be registered as "SansSerif" for UIFont fontWithName:size:
不过还是有一些问题。您需要知道字体名称,因为它可能与文件名稍有不同。在我的示例中,文件名是“Almagro Regular”。ttf,但是字体名称只是“Almagro”。在另一个论坛上,有人说,你需要注意名称中有空格的字体,因为像“SansSerif”这样的字体会被注册为“SansSerif”,用于UIFont fontWithName:size:
Let me know if it works.
如果有用,请告诉我。