我想在 NSTableView 中显示字体。如果我使用NSFont(name: fontName, size: size)
一切都可以的字体。但在这种情况下,我只能使用系统中安装的字体。所以我做了一个 NSFont 扩展:
public extension NSFont {
static func read(from path: String, size: CGFloat) throws -> NSFont {
guard let dataProvider = CGDataProvider(filename: path) else {
throw NSError(domain: "file not found", code: 77, userInfo: ["fileName" : path])
}
guard let fontRef = CGFont ( dataProvider ) else {
throw NSError(domain: "Not a font file", code: 77, userInfo: ["fileName" : path])
}
return CTFontCreateWithGraphicsFont(fontRef, size, nil, nil) as NSFont
}
}
它似乎有效,以这种方式制作的字体在[NSFont]
数组中找到了它们的位置。但是如果尝试将它们绑定到程序中的NSTextFieldCell
字体NSTableView
会爆炸:
(this goes forever and throws Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffff8))
......
......
#261509 0x00007fff6c023e63 in -[NSCTFont isEqual:] ()
#261510 0x00007fff4539908c in _CFNonObjCEqual ()
#261511 0x00007fff6c023e63 in -[NSCTFont isEqual:] ()
#261512 0x00007fff4539908c in _CFNonObjCEqual ()
#261513 0x00007fff6c023e63 in -[NSCTFont isEqual:] ()
#261514 0x00007fff6bfccf63 in -[NSAttributeDictionary isEqualToDictionary:] ()
#261515 0x00007fff6bfccc11 in attributeDictionaryIsEqual ()
#261516 0x00007fff475c6712 in hashProbe ()
#261517 0x00007fff475c6518 in -[NSConcreteHashTable getItem:] ()
#261518 0x00007fff6bfc5be8 in +[NSAttributeDictionary newWithDictionary:] ()
#261519 0x00007fff6bff7cbe in -[_NSCachedAttributedString initWithString:attributes:] ()
#261520 0x00007fff6bfdac1f in __NSStringDrawingEngine ()
#261521 0x00007fff6bff7380 in _NSStringDrawingCore ()
#261522 0x00007fff42b16477 in _NSDrawTextCell2 ()
#261523 0x00007fff42b15328 in __45-[NSTextFieldCell _drawForegroundOfTextLayer]_block_invoke ()
#261524 0x00007fff42a8c529 in -[NSFocusStack performWithFocusView:inWindow:usingBlock:] ()
#261525 0x00007fff42b14bff in -[NSTextFieldCell _drawForegroundOfTextLayer] ()
#261526 0x00007fff42b1445a in -[NSTextFieldCell updateLayerWithFrame:inView:] ()
#261527 0x00007fff42b14322 in -[NSControl updateLayer] ()
#261528 0x00007fff42afe301 in _NSViewUpdateLayer ()
......
......
我认为 CTFont 中缺少 NSFont 所拥有的东西。但是什么?