是否有不推荐的方法来访问OS X 10.11的字体集合?

时间:2015-08-12 16:53:18

标签: cocoa fonts

我正在考虑添加一个用户定义字体集合对可用字体进行排序的选项(我希望Pages和Keynote这样做!),但看起来在10.11中不推荐使用旧的访问这些集合的方式:

https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSFontManager_Class/index.html#//apple_ref/occ/clm/NSFontManager/

是否有新的方式来访问和使用这些字体集合?

2 个答案:

答案 0 :(得分:1)

有课程NSFontCollectionNSFontDescriptor

查看Xcode 7的NSFontManager头文件(通过⇧⌘O)以获取有关已弃用方法及其替代方法的更多信息。

答案 1 :(得分:1)

我最近一直在使用字体集合,所以我可能会为您提供一些信息。

NSFontCollection API有点奇怪。它可以访问"命名字体集合,"但是与所述集合相关联的名称并未附加到它们。如果这没有意义,那是因为它没有意义。但是,让我尝试将其分解:

添加系统范围的字体集合:

// Create a font descriptor (or descriptors) with whatever attributes you want
let descriptor = NSFontDescriptor(fontAttributes: nil)

// Create a font collection with the above descriptor(s)
let collection = NSFontCollection(descriptors: [descriptor])

// In Objective-C, the `+ showFontCollection:withName:visibility:error:`
// method returns `YES` if the collection was shown or `NO` if an error occurred.
// The error is passed via pointer supplied to the `error:` parameter.
//
// In Swift, the method returns `()` (aka nil literal), and instead of passing
// the error in a pointer, it `throws`, so you have to wrap the call in a `do`
// statement and catch the error (or suppress error propagation via `try!`):

do {
    try NSFontCollection.showFontCollection(collection: NSFontCollection, 
        withName: "Visible to All Users", visibility: .Computer)
}
catch {
    print("There was an error showing font collection. Info: \(error)")
}

添加用户可见的字体集合:

重复上述步骤,用.Computer替换.User

do {
    try NSFontCollection.showFontCollection(collection: NSFontCollection, 
        withName: "Visible to the Current User", visibility: .User)
}
catch {
    print("There was an error showing font collection. Info: \(error)")
}

添加非持久性字体集合:

重复上述步骤,用.Computer替换.Process

do {
    try NSFontCollection.showFontCollection(collection: NSFontCollection, 
        withName: "Visible for Current Process", visibility: .Process)
}
catch {
    print("There was an error showing font collection. Info: \(error)")
}

后续步骤......

收藏完成后,您可以使用NSMutableFontCollection课程更改所有内容。继续上面的例子,你会做这样的事情:

let mutableCollection = collection.mutableCopy() as! NSMutableFontCollection

let boldTrait = [NSFontWeightTrait: NSFontWeightBold]
let boldAttributes = [NSFontTraitsAttribute: boldTrait]
let boldDescriptor = NSFontDescriptor(fontAttributes: newAttributes)

mutableCollection.addQueryForDescriptors([boldDescriptor])

此时,API再次变得怪异。我们已经为我们的"命名集合添加了描述符,"但是,除非你显示"否则UI中的任何内容都不会显示出来。字体集再次。换句话说,在进行任何更改后,您必须再次致电showFontCollection(_:withName:visibility:)

同样,如果要删除/删除某个集合,则必须调用hideFontCollectionWithName(_:visibility:)。尽管名称无关紧要,但这种方法完全从磁盘中删除了一个持久集合,所以要小心。

下一步后续步骤......

在您的应用的后续发布中,您可以使用NSFontCollection(name:visibility:)方法检索任何持久性集合,如下所示:

// Retrieve collection created at an earlier time
let collectionOnDisk = NSFontCollection(name: "Visible to All Users", visibility: .Computer)

我认为我已经涵盖了大部分内容,但如果我错过了某些内容,或者您​​有疑问,请告诉我。祝你好运!