如何从NSFontAttributeName中提取字体名称



我有一个NSComboBox,我希望它显示光标所在的字体名称。我有当前代码:

- (void)textDidChange:(NSNotification *)notification {
    [self.fontBox setStringValue:[self.doc.textStorage attribute:NSFontAttributeName atIndex:(self.doc.selectedRange.location - 1) effectiveRange:nil]];
} 

它将组合框的标题设置为字体,但我得到了类似于:"ArialMT 12.00 pt. P [] (0x1001a95b0) fobj=0x1006511c0, spc=3.33"的内容。我只想得到Arial、Times New Roman或Helvetica,而不是那个长字符串。我该怎么做?

使用NSFontdisplayName属性。

NSFont *font = [self.doc.textStorage attribute:NSFontAttributeName atIndex:(self.doc.selectedRange.location - 1) effectiveRange:nil];
[self.fontBox setStringValue:font.displayName];

最新更新