iOS自定义字体的获取

从本地读取字体 并注册 

读取方法一 :

+ (instancetype)customFont:(CGFloat)fontSize
{
    NSString *fontPath = [[NSBundle mainBundle]pathForResource:@"regular" ofType:@"otf"];
    NSData *dynamicFontData = [NSData dataWithContentsOfFile:fontPath];
    if (!dynamicFontData){
        return [self customFont:fontSize];//系统自带字体
    }
    CFErrorRef error;
    CGDataProviderRef providerRef = CGDataProviderCreateWithCFData((__bridge CFDataRef)dynamicFontData);
    CGFontRef newFont = CGFontCreateWithDataProvider(providerRef);
    CFStringRef fontName = CGFontCopyFullName(newFont);
    NSString *fontNameString = (__bridge id)(fontName);
    if ( ![self isResisted:fontNameString] && ! CTFontManagerRegisterGraphicsFont(newFont, &error)){
        //注册失败
        CFStringRef errorDescription = CFErrorCopyDescription(error);
        DDLog(@"自定义字体注册失败: %@", errorDescription);
        CFRelease(errorDescription);
    }
    
    CFRelease(newFont);
    CFRelease(providerRef);
    CFRelease(fontName);
    
    UIFont *fone = [UIFont  fontWithName:fontNameString size:fontSize];
    if (!fone){
        fone = [self customFont:fontSize];//系统自带字体
    }
    return fone;
}

读取方法二 (参考http://www.xuebuyuan.com/2189874.html) :

NSString *fontPath = [[NSBundle mainBundle] pathForResource:fontFileNameArray[i] ofType:nil];  
NSURL *url = [NSURL fileURLWithPath:fontPath];  
  
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)url);  
if (fontDataProvider == NULL)  
{  
    break;  
}  
  
CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);  
CGDataProviderRelease(fontDataProvider);  
if (newFont == NULL)  
{  
    break;  
}  
CFStringRef fontName = CGFontCopyFullName(newFont);  
  
NSString *fontNameString = (__bridge id)(fontName);  
if ([fontNameString isEqualToString:@"Yuppy SC Regular"]) {  
    fontNameString = @"YuppySC-Regular";  
}  
[fontNameMDic setObject:fontNameString forKey:fontFileNameArray[i]];  
  
CFRelease(fontName);  
CGFontRelease(newFont); 

读取方法三(参考http://www.cnblogs.com/vicstudio/p/3961195.html):

-(UIFont*)customFontWithPath:(NSString*)path size:(CGFloat)size
{
    NSURL *fontUrl = [NSURL fileURLWithPath:path];
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fontUrl);
    CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
    CGDataProviderRelease(fontDataProvider);
    CTFontManagerRegisterGraphicsFont(fontRef, NULL);
    NSString *fontName = CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
    UIFont *font = [UIFont fontWithName:fontName size:size];
    CGFontRelease(fontRef);
    return font;
}

以上三种方法可以读取ttf,otf 格式字体,但是方法一、二发现有时会有读取不到的情况,读取不到的原因是fontNameString 获取错误,方法一有时获取到的fontNameString是Yuppy SC Regular 但是实际的fontNameString  是 YuppySC-Regular,方法二的缺点是要事先知道fontNameString。方法三还没有发现读取不到的情况。

注: 方法一、二获取字体名的方法是:CGFontCopyFullName 方法三取字体名的方法是CGFontCopyPostScriptName,方法一 二获取的FullName,iOS用的是PostScriptName

 

TTC字体的读取方法(见http://www.cnblogs.com/vicstudio/p/3961195.html):

-(NSArray*)customFontArrayWithPath:(NSString*)path size:(CGFloat)size
{
    CFStringRef fontPath = CFStringCreateWithCString(NULL, [path UTF8String], kCFStringEncodingUTF8);
    CFURLRef fontUrl = CFURLCreateWithFileSystemPath(NULL, fontPath, kCFURLPOSIXPathStyle, 0);
    CFArrayRef fontArray =CTFontManagerCreateFontDescriptorsFromURL(fontUrl);
    CTFontManagerRegisterFontsForURL(fontUrl, kCTFontManagerScopeNone, NULL);
    NSMutableArray *customFontArray = [NSMutableArray array];
    for (CFIndex i = 0 ; i < CFArrayGetCount(fontArray); i++){
        CTFontDescriptorRef  descriptor = CFArrayGetValueAtIndex(fontArray, i);
        CTFontRef fontRef = CTFontCreateWithFontDescriptor(descriptor, size, NULL);
        NSString *fontName = CFBridgingRelease(CTFontCopyName(fontRef, kCTFontPostScriptNameKey));
        UIFont *font = [UIFont fontWithName:fontName size:size];
        [customFontArray addObject:font];
    }
    
    return customFontArray;
}

 

不过这个方法只支持7.0以上,暂时在7.0以下没有找到方法。

个人看法,因为ttc里面的字体都比较相似,所以其实使用一个也足以。

 

附:(字体的介绍)

TTF(TrueTypeFont)是一种字库名称。TTF(TrueTypeFont)是Apple公司和Microsoft公司共同推出的字体文件格式,随着windows的流行,已经变成最常用的一种字体文件表示方式。

TTC字体是TrueType字体集成文件(. TTC文件),是在一单独文件结构中包含多种字体,以便更有效地共享轮廓数据,当多种字体共享同一笔画时,TTC技术可有效地减小字体文件的大小。
TTC是几个TTF合成的字库,安装后字体列表中会看到两个以上的字体。两个字体中大部分字都一样时,可以将两种字体做成一个TTC文件,常见的TTC字体,因为共享笔划数据,所以大多这个集合中的字体区别只是字符宽度不一样,以便适应不同的版面排版要求。
 
而TTF字体则只包含一种字型。

猜你喜欢

转载自blog.csdn.net/wang_Bo_JustOne/article/details/80393386