仅在测试目标中使用自定义字体?

时间:2014-10-30 15:26:24

标签: ios xcode uifont

我想在测试目标中使用iOS中的自定义字体。 仅在测试目标中。

使用案例:我使用XCTest目标从我们的应用生成屏幕截图。我想用自定义字体添加一些文本。

所以我需要在测试目标中使用自定义字体,但不能在主应用程序中使用。

有任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

要从测试目标加载字体,只需在测试包内运行:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:fontFileName ofType:@"ttf"];
NSData *inData = [NSData dataWithContentsOfFile:path];

CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (!CTFontManagerRegisterGraphicsFont(font, &error)) {
    CFStringRef errorDescription = CFErrorCopyDescription(error);
    NSLog(@"Error loading font: %@", errorDescription);
    CFRelease(errorDescription);
}
CFRelease(font);