経由で登録したフォントのリストを取得する:iOSのCoreTextは、私は、次の経由で動的にフォントを登録していますCTFontManagerRegisterGraphicsFont
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if ([url isFileURL])
{
// Handle file being passed in
NSLog(@"handleOpenURL: %@",url.absoluteString);
NSData *inData = [NSData dataWithContentsOfURL:url];
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef fontRef = CGFontCreateWithDataProvider(provider);
UIFont *font;
if (!CTFontManagerRegisterGraphicsFont(fontRef, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", error);
CFRelease(errorDescription);
} else {
CFStringRef fontNameRef = CGFontCopyPostScriptName(fontRef);
NSLog(@"fontNameRef: %@",fontNameRef);
font = [UIFont fontWithName:(__bridge NSString *)fontNameRef size:80];
[self.arrayOfFonts addObject:(__bridge NSString *)fontNameRef];
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshFont" object:nil];
CFRelease(fontNameRef);
}
CFRelease(fontRef);
CFRelease(provider);
return YES;
}
else
{
return NO;
}
}
それは細かい初めての作品。アプリを閉じてもう一度同じフォントを登録しようとすると、(予想される)エラーが表示されるようです。"Failed to load font: Error Domain=com.apple.CoreText.CTFontManagerErrorDomain Code=105 "Could not register the CGFont '<CGFont (0x1c00f5980): NeuropolXRg-Regular>'" UserInfo={NSDescription=Could not register the CGFont '<CGFont (0x1c00f5980): NeuropolXRg-Regular>', CTFailedCGFont=<CGFont (0x1c00f5980): NeuropolXRg-Regular>}"
これはフォントが既に登録されているためです。 CTFontManagerRegisterGraphicsFont
のドキュメントには、
"フォントマネージャーで登録されたフォントが登録されています。登録されているフォントは、フォント記述子の照合によって検出可能です。すでに登録されているフォントは失敗します。
「フォント記述子のマッチング」を正確に行う方法
CTFontManagerRegisterGraphicsFont
メソッドで登録されたすべてのフォントのリストを取得するには、どうすれば登録前に登録を解除できますか?
EDIT:
私はCTFontManagerCopyAvailablePostScriptNames
とCTFontManagerCopyAvailableFontFamilyNames
メソッドを使用してみましたが、両方の唯一のiOSですでに利用可能なフォントの名前をプリントアウトしてきました。私はCTFontManagerRegisterGraphicsFont
NOTE経由で登録されたものではない:は、私は[UIFont familyNames]を反復処理によって記載されていることができるのiOSで利用できる、すでにフォントについて尋ねていません。
- (void)getInstalledFonts {
NSDictionary *descriptorOptions = @{(id)kCTFontDownloadableAttribute : @YES};
CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)descriptorOptions);
CFArrayRef fontDescriptors = CTFontDescriptorCreateMatchingFontDescriptors(descriptor, NULL);
[self showExistingFonts:(NSArray *)CFBridgingRelease(fontDescriptors)];
CFRelease(descriptor);
}
- (void)showExistingFonts:(NSArray *)fontList {
NSMutableDictionary *fontFamilies = [NSMutableDictionary new];
for(UIFontDescriptor *descriptor in fontList) {
NSString *fontFamilyName = [descriptor objectForKey:UIFontDescriptorFamilyAttribute];
NSMutableArray *fontDescriptors = [fontFamilies objectForKey:fontFamilyName];
if(!fontDescriptors) {
fontDescriptors = [NSMutableArray new];
[fontFamilies setObject:fontDescriptors forKey:fontFamilyName];
}
[fontDescriptors addObject:descriptor];
}
}
から撮影:あなたが使用する必要がありますどのような
誰かが私を助けてくれますか? –
「アプリを閉じる」とまったく同じ意味ですか? –
また、['CTFontManagerCopyAvailablePostScriptNames'](https://developer.apple.com/documentation/coretext/1499516-ctfontmanagercopyavailablepostsc)を試しましたか? –