我想在测试目标中使用 iOS 中的自定义字体。 仅在测试目标中。
用例:我使用 XCTest 目标从我们的应用生成屏幕截图。我喜欢添加一些带有自定义字体的文本。
所以我需要在测试目标中使用自定义字体,但在主应用程序中不需要。
有什么办法可以解决这个问题吗?
最佳答案
要从测试目标加载字体,只需在测试包中运行:
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);
关于ios - 仅在测试目标中使用自定义字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26657028/