iOS CoreText 通过 CTFontManagerRegisterGraphicsFont 获取注册字体列表
iOS CoreText get list of registered fonts via 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
的文档指出它:
"Registers the specified graphics font with the font manager. Registered fonts are discoverable through font descriptor matching. Attempts to register a font that is either already registered or contains the same PostScript name of an already registered font will fail."
具体怎么做"through font descriptor matching"??
如何获取已通过 CTFontManagerRegisterGraphicsFont
方法注册的所有字体的列表,以便在再次注册之前取消注册它们?
编辑:
我已经尝试使用 CTFontManagerCopyAvailablePostScriptNames
和 CTFontManagerCopyAvailableFontFamilyNames
方法,但两者都只打印出 iOS 上已经可用的字体名称。不是我通过 CTFontManagerRegisterGraphicsFont
注册的那些
注意: 我不是在询问 iOS 上已经可用的字体,这些字体可以通过遍历 [UIFont familyNames] 列出。
你应该使用的是这样的:
- (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];
}
}
取自:
https://www.shinobicontrols.com/blog/ios7-day-by-day-day-22-downloadable-fonts
我向 Apple DTS(开发者技术支持)提交了一张票,他们说:
"You need to keep track of the fonts you have registered using CTFontManagerRegisterGraphicsFont yourself. The error code kCTFontManagerErrorAlreadyRegistered returned by CTFontManagerRegisterGraphicsFont will tell you if you have already registered a font. Using font descriptor matching to discover if your fonts are installed probably isn’t a good way to go as the system may choose to perform font substitution for missing fonts. Installing a font using CTFontManagerRegisterGraphicsFont simply makes it available for your app’s use. It is not a query service for discovering installed fonts. If that is not sufficient for your preferences, then I think it would be a good idea for you to consider filing a feature request asking for the functionality you would like to receive."
所以基本上,我们需要跟踪我们自己注册的字体。
Solution/Work-around 我最终使用了:
目前,我的应用程序允许用户使用字体文件上的 Action Sheet's
"Copy to MYAPP" 按钮添加字体。同样的解决方案也适用于我从服务器下载的字体文件。
为了让我的应用在 .ttf
和 .otf
文件的操作 Sheet 中列出,在我的应用的 info.plist 中,我添加了一个新的文档类型:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>Font</string>
<key>LSItemContentTypes</key>
<array>
<string>public.opentype-font</string>
<string>public.truetype-ttf-font</string>
</array>
</dict>
</array>
这允许我的应用程序显示在任何字体文件的 action sheet
中。因此用户可以将字体文件放在 dropbox、google 驱动器或任何其他文件共享应用程序中。然后他们可以从那里将字体导入我的应用程序。导入后,我需要将字体文件从临时 tmp
inbox
文件夹移动到我应用程序的 Documents
fonts
目录。 fonts
目录保留所有自定义字体。之后,我注册了这个自定义字体并将名称添加到 self.arrayOfFonts
数组中。这是包含我所有字体列表的数组。
而且 CTFontManagerRegisterGraphicsFont
似乎仅适用于会话。因此,当应用程序从 App Switcher 关闭并重新启动时,该字体将不再注册。所以每次启动后,我都会浏览我的 documents/fonts
文件夹并重新注册所有字体并将它们的名称添加到 self.arrayOfFonts
数组中。
我的应用代码的其余部分使它正常工作:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if ([url isFileURL])
{
// Handle file being passed in
NSLog(@"handleOpenURL: %@, extension: %@",url.absoluteString,url.pathExtension);
[self moveFontFrom:url];
return YES;
}
else
{
return NO;
}
}
-(void)moveFontFrom:(NSURL*)fromurl{
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:@"fonts"];
// New Folder is your folder name
NSError *error1 = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath]){
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error1];
}
NSLog(@"error1: %@", error1.debugDescription);
NSURL *tourl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",stringPath,[[fromurl absoluteString] lastPathComponent]] isDirectory:NO];
NSLog(@"Trying to move from:\n\n%@\n\nto:\n\n%@\n\n", fromurl.absoluteString,tourl.absoluteString);
NSError* error2;
if ([[NSFileManager defaultManager] fileExistsAtPath:tourl.path]){
[[NSFileManager defaultManager] removeItemAtPath:tourl.path error:&error2];
NSLog(@"Deleting old existing file at %@ error2: %@", tourl.path,error2.debugDescription);
}
NSError* error3;
[[NSFileManager defaultManager] moveItemAtURL:fromurl toURL:tourl error:&error3];
NSLog(@"error3: %@", error3.debugDescription);
if (!error3) {
NSString *fontName = [self registerFont:tourl checkIfNotify:YES];
if (fontName) {
if (![self.arrayOfFonts containsObject:fontName]) {
[self.arrayOfFonts addObject:fontName];
[self.arrayOfFonts sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshFont" object:nil userInfo:@{@"font":fontName}];
}
}
}
}
-(void)startupLoadFontsInDocuments{
self.arrayOfFonts = [NSMutableArray new];
NSString *location = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:@"fonts"];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:location error:NULL];
for (NSInteger count = 0; count < [directoryContent count]; count++)
{
NSLog(@"File %ld: %@/%@", (count + 1), location,[directoryContent objectAtIndex:count]);
NSString *fontName = [self registerFont:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",location,[directoryContent objectAtIndex:count]] isDirectory:NO] checkIfNotify:NO];
if (fontName) {
if (![self.arrayOfFonts containsObject:fontName]) {
[self.arrayOfFonts addObject:fontName];
}
}
}
[self.arrayOfFonts sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
-(NSString*)registerFont:(NSURL *)url checkIfNotify:(BOOL)checkIfNotify{
NSData *inData = [NSData dataWithContentsOfURL:url];
CFErrorRef registererror;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef fontRef = CGFontCreateWithDataProvider(provider);
NSString *fontName = (__bridge NSString *)CGFontCopyPostScriptName(fontRef);
BOOL registerFontStatus = CTFontManagerRegisterGraphicsFont(fontRef, ®istererror);
if (!registerFontStatus) {
CFStringRef errorDescription = CFErrorCopyDescription(registererror);
NSError *registererr = (__bridge NSError*)registererror;
if ([registererr code]==kCTFontManagerErrorAlreadyRegistered) {
NSLog(@"Font is already registered!");
}
NSLog(@"Failed to load font: %@", registererror);
CFRelease(errorDescription);
/*CFErrorRef unregistererror;
BOOL unregisterFont = CTFontManagerUnregisterGraphicsFont(fontRef, &unregistererror);
NSLog(@"Font unregister status: %d",unregisterFont);
CFStringRef unregistererrorDescription = CFErrorCopyDescription(unregistererror);
NSError *unregistererr = (__bridge NSError*)unregistererror;
NSInteger code = [unregistererr code];
NSLog(@"Failed to unregister font: %@", unregistererr);
CFRelease(unregistererrorDescription);*/
if (checkIfNotify) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Already added" message:@"That font is already added to the app. Please select it from the fonts list." preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[[self.window rootViewController] presentViewController:alert animated:YES completion:nil];
}
} else {
CFStringRef fontNameRef = CGFontCopyPostScriptName(fontRef);
fontName = (__bridge NSString*)fontNameRef;
CFRelease(fontNameRef);
NSLog(@"fontName: %@",fontName);
}
CFRelease(fontRef);
CFRelease(provider);
return fontName;
}
注意: 如您所见,我已经注释掉了 CTFontManagerUnregisterGraphicsFont
。用于取消注册字体的 CTFontManagerUnregisterGraphicsFont
似乎对我不起作用,因为它给出了一个错误,指出字体正在使用中,因此无法取消注册。因此,当我需要删除字体时,我只需将其从 self.arrayOfFonts
数组和我的 documents/fonts
文件夹中删除即可。
我发现,如果您曾经将 UIFont(name:) 与您动态加载的字体一起使用。 CTFontManagerUnregisterFontsForURL 不能如上所述工作。
所以解决方法是使用
let desc = UIFontDescriptor(name: <name>, size: <size>)
let font = UIFont(descriptor: desc, size: <size>)
如果您在任何时候使用 UIFont(name: ),注销并不会真正注销它。
不 我发现 CTFontManager Register/Unregstier FontsForURL api 和 GraphicsFont apis.
就是这种情况
我正在通过以下方式动态注册字体:
- (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
的文档指出它:
"Registers the specified graphics font with the font manager. Registered fonts are discoverable through font descriptor matching. Attempts to register a font that is either already registered or contains the same PostScript name of an already registered font will fail."
具体怎么做"through font descriptor matching"??
如何获取已通过 CTFontManagerRegisterGraphicsFont
方法注册的所有字体的列表,以便在再次注册之前取消注册它们?
编辑:
我已经尝试使用 CTFontManagerCopyAvailablePostScriptNames
和 CTFontManagerCopyAvailableFontFamilyNames
方法,但两者都只打印出 iOS 上已经可用的字体名称。不是我通过 CTFontManagerRegisterGraphicsFont
注意: 我不是在询问 iOS 上已经可用的字体,这些字体可以通过遍历 [UIFont familyNames] 列出。
你应该使用的是这样的:
- (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];
}
}
取自: https://www.shinobicontrols.com/blog/ios7-day-by-day-day-22-downloadable-fonts
我向 Apple DTS(开发者技术支持)提交了一张票,他们说:
"You need to keep track of the fonts you have registered using CTFontManagerRegisterGraphicsFont yourself. The error code kCTFontManagerErrorAlreadyRegistered returned by CTFontManagerRegisterGraphicsFont will tell you if you have already registered a font. Using font descriptor matching to discover if your fonts are installed probably isn’t a good way to go as the system may choose to perform font substitution for missing fonts. Installing a font using CTFontManagerRegisterGraphicsFont simply makes it available for your app’s use. It is not a query service for discovering installed fonts. If that is not sufficient for your preferences, then I think it would be a good idea for you to consider filing a feature request asking for the functionality you would like to receive."
所以基本上,我们需要跟踪我们自己注册的字体。
Solution/Work-around 我最终使用了:
目前,我的应用程序允许用户使用字体文件上的 Action Sheet's
"Copy to MYAPP" 按钮添加字体。同样的解决方案也适用于我从服务器下载的字体文件。
为了让我的应用在 .ttf
和 .otf
文件的操作 Sheet 中列出,在我的应用的 info.plist 中,我添加了一个新的文档类型:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>Font</string>
<key>LSItemContentTypes</key>
<array>
<string>public.opentype-font</string>
<string>public.truetype-ttf-font</string>
</array>
</dict>
</array>
这允许我的应用程序显示在任何字体文件的 action sheet
中。因此用户可以将字体文件放在 dropbox、google 驱动器或任何其他文件共享应用程序中。然后他们可以从那里将字体导入我的应用程序。导入后,我需要将字体文件从临时 tmp
inbox
文件夹移动到我应用程序的 Documents
fonts
目录。 fonts
目录保留所有自定义字体。之后,我注册了这个自定义字体并将名称添加到 self.arrayOfFonts
数组中。这是包含我所有字体列表的数组。
而且 CTFontManagerRegisterGraphicsFont
似乎仅适用于会话。因此,当应用程序从 App Switcher 关闭并重新启动时,该字体将不再注册。所以每次启动后,我都会浏览我的 documents/fonts
文件夹并重新注册所有字体并将它们的名称添加到 self.arrayOfFonts
数组中。
我的应用代码的其余部分使它正常工作:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if ([url isFileURL])
{
// Handle file being passed in
NSLog(@"handleOpenURL: %@, extension: %@",url.absoluteString,url.pathExtension);
[self moveFontFrom:url];
return YES;
}
else
{
return NO;
}
}
-(void)moveFontFrom:(NSURL*)fromurl{
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:@"fonts"];
// New Folder is your folder name
NSError *error1 = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath]){
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error1];
}
NSLog(@"error1: %@", error1.debugDescription);
NSURL *tourl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",stringPath,[[fromurl absoluteString] lastPathComponent]] isDirectory:NO];
NSLog(@"Trying to move from:\n\n%@\n\nto:\n\n%@\n\n", fromurl.absoluteString,tourl.absoluteString);
NSError* error2;
if ([[NSFileManager defaultManager] fileExistsAtPath:tourl.path]){
[[NSFileManager defaultManager] removeItemAtPath:tourl.path error:&error2];
NSLog(@"Deleting old existing file at %@ error2: %@", tourl.path,error2.debugDescription);
}
NSError* error3;
[[NSFileManager defaultManager] moveItemAtURL:fromurl toURL:tourl error:&error3];
NSLog(@"error3: %@", error3.debugDescription);
if (!error3) {
NSString *fontName = [self registerFont:tourl checkIfNotify:YES];
if (fontName) {
if (![self.arrayOfFonts containsObject:fontName]) {
[self.arrayOfFonts addObject:fontName];
[self.arrayOfFonts sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshFont" object:nil userInfo:@{@"font":fontName}];
}
}
}
}
-(void)startupLoadFontsInDocuments{
self.arrayOfFonts = [NSMutableArray new];
NSString *location = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:@"fonts"];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:location error:NULL];
for (NSInteger count = 0; count < [directoryContent count]; count++)
{
NSLog(@"File %ld: %@/%@", (count + 1), location,[directoryContent objectAtIndex:count]);
NSString *fontName = [self registerFont:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",location,[directoryContent objectAtIndex:count]] isDirectory:NO] checkIfNotify:NO];
if (fontName) {
if (![self.arrayOfFonts containsObject:fontName]) {
[self.arrayOfFonts addObject:fontName];
}
}
}
[self.arrayOfFonts sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
-(NSString*)registerFont:(NSURL *)url checkIfNotify:(BOOL)checkIfNotify{
NSData *inData = [NSData dataWithContentsOfURL:url];
CFErrorRef registererror;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef fontRef = CGFontCreateWithDataProvider(provider);
NSString *fontName = (__bridge NSString *)CGFontCopyPostScriptName(fontRef);
BOOL registerFontStatus = CTFontManagerRegisterGraphicsFont(fontRef, ®istererror);
if (!registerFontStatus) {
CFStringRef errorDescription = CFErrorCopyDescription(registererror);
NSError *registererr = (__bridge NSError*)registererror;
if ([registererr code]==kCTFontManagerErrorAlreadyRegistered) {
NSLog(@"Font is already registered!");
}
NSLog(@"Failed to load font: %@", registererror);
CFRelease(errorDescription);
/*CFErrorRef unregistererror;
BOOL unregisterFont = CTFontManagerUnregisterGraphicsFont(fontRef, &unregistererror);
NSLog(@"Font unregister status: %d",unregisterFont);
CFStringRef unregistererrorDescription = CFErrorCopyDescription(unregistererror);
NSError *unregistererr = (__bridge NSError*)unregistererror;
NSInteger code = [unregistererr code];
NSLog(@"Failed to unregister font: %@", unregistererr);
CFRelease(unregistererrorDescription);*/
if (checkIfNotify) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Already added" message:@"That font is already added to the app. Please select it from the fonts list." preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[[self.window rootViewController] presentViewController:alert animated:YES completion:nil];
}
} else {
CFStringRef fontNameRef = CGFontCopyPostScriptName(fontRef);
fontName = (__bridge NSString*)fontNameRef;
CFRelease(fontNameRef);
NSLog(@"fontName: %@",fontName);
}
CFRelease(fontRef);
CFRelease(provider);
return fontName;
}
注意: 如您所见,我已经注释掉了 CTFontManagerUnregisterGraphicsFont
。用于取消注册字体的 CTFontManagerUnregisterGraphicsFont
似乎对我不起作用,因为它给出了一个错误,指出字体正在使用中,因此无法取消注册。因此,当我需要删除字体时,我只需将其从 self.arrayOfFonts
数组和我的 documents/fonts
文件夹中删除即可。
我发现,如果您曾经将 UIFont(name:) 与您动态加载的字体一起使用。 CTFontManagerUnregisterFontsForURL 不能如上所述工作。
所以解决方法是使用
let desc = UIFontDescriptor(name: <name>, size: <size>)
let font = UIFont(descriptor: desc, size: <size>)
如果您在任何时候使用 UIFont(name: ),注销并不会真正注销它。
不 我发现 CTFontManager Register/Unregstier FontsForURL api 和 GraphicsFont apis.
就是这种情况