Macos 如何在OS X上检索已卸载字体的属性?
我正在尝试读取已卸载的字体的属性(字体名称、字体格式等) 我已从桌面上的Macos 如何在OS X上检索已卸载字体的属性?,macos,fonts,core-text,Macos,Fonts,Core Text,我正在尝试读取已卸载的字体的属性(字体名称、字体格式等) 我已从桌面上的/System/Library/Fonts/Menlo.ttc复制了Menlo.ttc文件,并已下载并安装了该字体 我尝试了以下代码: #导入 #进口 静态无效printFontInfo(NSURL*fontURL) { NSLog(@“%@,[fontURL路径]); NSLog(@“文件大小:%lld”,[[NSFileManager defaultManager]AttributesFiteMatPath:[fontU
/System/Library/Fonts/Menlo.ttc
复制了Menlo.ttc
文件,并已下载并安装了该字体
我尝试了以下代码:
#导入
#进口
静态无效printFontInfo(NSURL*fontURL)
{
NSLog(@“%@,[fontURL路径]);
NSLog(@“文件大小:%lld”,[[NSFileManager defaultManager]AttributesFiteMatPath:[fontURL路径]错误:NULL]fileSize]);
NSDictionary*attributes=@{(id)kCTFontURLAttribute:fontURL};
CTFontDescriptorRef fontDescriptor=CTFontDescriptorCreateWithAttributes((_桥CFDictionarRef)属性);
CFNumberRef format=CTFontDescriptorCopyAttribute(fontDescriptor,kCTFontFormatAttribute);
CFStringRef fontName=CTFontDescriptorCopyAttribute(fontDescriptor,kCTFontNameAttribute);
CFStringRef displayName=CTFontDescriptorCopyAttribute(fontDescriptor,kCTFontDisplayNameAttribute);
NSLog(@“fontDescriptor:%p”,fontDescriptor);
NSLog(@“格式:%@”,格式);
NSLog(@“字体名称:%@”,字体名称);
NSLog(@“显示名称:%@”,显示名称);
}
int main(int argc,const char*argv[]
{
@自动释放池
{
NSFileManager*fileManager=[NSFileManager defaultManager];
NSURL*desktopURL=[fileManager URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask APPLICATEFORURL:nil create:NO error:NULL];
NSURL*downloadsURL=[fileManager URLForDirectory:NSDownloadsDirectory inDomain:NSUserDomainMask ApprovedForURL:nil创建:无错误:NULL];
NSURL*fontsURL=[[fileManager URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask ApproverForURL:nil创建:无错误:NULL]URLByAppendingPathComponent:@“Fonts”];
NSURL*menloURL=[NSURL fileURLWithPath:@”/System/Library/Fonts/Menlo.ttc];
NSURL*copiedMenloURL=[desktopURL URLByAppendingPathComponent:@“Menlo.ttc”];
NSURL*installedCaviarDreamsURL=[fontsURL URLByAppendingPathComponent:@“CaviarDreams.ttf]”;
NSURL*downloadedCaviarDreamsURL=[downloadsURL URLByAppendingPathComponent:@“caviar_dreams/CaviarDreams.ttf”];
printFontInfo(menloURL);
printFontInfo(复印件孟鲁);
printFontInfo(已安装鱼子酱DreamSurl);
printFontInfo(下载鱼子酱DreamSurl);
}
返回0;
}
运行此代码会产生以下结果:
/System/Library/Fonts/Menlo.ttc
file size: 2144600
fontDescriptor: 0x100111550
format: 3
font name: Menlo-Regular
display name: Menlo Regular
/Users/0xced/Desktop/Menlo.ttc
file size: 2144600
fontDescriptor: 0x100117fa0
format: (null)
font name: (null)
display name: (null)
/Users/0xced/Library/Fonts/CaviarDreams.ttf
file size: 58864
fontDescriptor: 0x100118f30
format: 3
font name: CaviarDreams
display name: Caviar Dreams
/Users/0xced/Downloads/caviar_dreams/CaviarDreams.ttf
file size: 58864
fontDescriptor: 0x1001186c0
format: (null)
font name: (null)
display name: (null)
我们看到在安装字体时可以读取属性(在/System/Library/font
或~/Library/font
中),但当字体不在系统字体目录中时,ctfontscriptorcopyattribute
函数会失败
有没有办法在未安装字体时读取字体属性?您必须使用
ctfontmanagercreatefontsdescriptorsfromurl(fontURL)
而不是ctfontsdescriptorcreatewithattributes(@{(id)kctfonturldattribute:fontURL})
。ctfontmanagercreatefontsdescriptorsfromurl
函数返回ctfontsdescriptorref
对象数组。鱼子酱梦
好名字。。。