我想允许我的应用程序用户在应用程序中使用自己的字体,方法是将它们复制到Documents目录(通过iTunes)中。但是,我找不到以这种方式使用自定义字体的方法,因为正确的方式取决于在应用程序的Info.plist中使用UIAppFonts密钥。iOS:在运行时以编程方式添加自定义字体
有什么办法可以在运行时覆盖这个吗?
谢谢。
我想允许我的应用程序用户在应用程序中使用自己的字体,方法是将它们复制到Documents目录(通过iTunes)中。但是,我找不到以这种方式使用自定义字体的方法,因为正确的方式取决于在应用程序的Info.plist中使用UIAppFonts密钥。iOS:在运行时以编程方式添加自定义字体
有什么办法可以在运行时覆盖这个吗?
谢谢。
有一个由Zynga的家伙创建的类,可以加载任何自定义字体:FontLabel。
您必须在您的应用程序启动时(例如在您的应用程序委托中)为要在您的应用程序中使用的每种字体调用[FontManager loadFont:]
。
因此,在Documents文件夹中寻找.ttf文件(该库仅与ttf字体一起使用)中进行迭代是不平凡的。
有一点要注意:这个类使用UILabel的子类。
我知道这是一个老问题,但我今天想要做同样的事情,并找到了使用CoreText和CGFont的方法。
首先要确保您添加CoreText框架和
#import <CoreText/CoreText.h>
那么这应该这样做(在这个例子中,我使用我之前下载并保存到文档目录的fonts目录中的字体):
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * fontPath = [documentsDirectory stringByAppendingPathComponent:@"Fonts/Chalkduster.ttf"];
NSURL * url = [NSURL fileURLWithPath:fontPath];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)url);
CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
NSString * newFontName = (__bridge NSString *)CGFontCopyPostScriptName(newFont);
CGDataProviderRelease(fontDataProvider);
CFErrorRef error;
CTFontManagerRegisterGraphicsFont(newFont, &error);
CGFontRelease(newFont);
UIFont * finalFont = [UIFont fontWithName:newFontName size:20.0f];
希望它可以帮助任何人绊倒这个问题!
我使用这种方法,谢谢 – 2013-03-27 09:22:44
试试这个
#import "MBProgressHUD.h"
#import <CoreText/CoreText.h>
- (void)viewDidLoad
{
NSURL *fileNameURL=[NSURL URLWithString:@"http://www.ge.tt/api/1/files/6d7jEnk/0/"];
NSMutableURLRequest *filenameReq=[[NSMutableURLRequest alloc] initWithURL:fileNameURL];
NSData *responseData=[NSURLConnection sendSynchronousRequest:filenameReq returningResponse:nil error:nil];
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:nil];
NSString *fontFileName=[[[json valueForKey:@"filename"] componentsSeparatedByString:@"."] objectAtIndex:0];
NSLog(@"file name is %@",fontFileName);
NSURL *url=[NSURL URLWithString:@"http://www.ge.tt/api/1/files/6d7jEnk/0/blob?download"];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:url];
__block NSError *error;
__block NSURLResponse *response;
MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[email protected]"Changing Font..";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *rootPath=[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents"]];
NSString *filePath=[rootPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.ttf",fontFileName]];
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
NSFileManager *fm=[NSFileManager defaultManager];
if (![fm fileExistsAtPath:filePath]) {
[urlData writeToFile:filePath atomically:YES];
}
NSString *rootPath=[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents"]];
NSString *filePath=[rootPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.ttf",fontFileName]];
NSURL * fonturl = [NSURL fileURLWithPath:filePath];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fonturl);
CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
NSString * newFontName = (__bridge NSString *)CGFontCopyPostScriptName(newFont);
CGDataProviderRelease(fontDataProvider);
CFErrorRef fonterror;
CTFontManagerRegisterGraphicsFont(newFont, &fonterror);
CGFontRelease(newFont);
UIFont * finalFont = [UIFont fontWithName:newFontName size:20.0f];
[txt_UserName setFont:finalFont];
});
});
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
它看起来像
谢谢,这看起来相当有效的,但我希望一个更好的解决方案。毕竟我使用UITextView来显示文本。 – Vicarius 2011-02-09 09:07:22