Search code examples
objective-ccocoanscombobox

How do I extract the font name from a NSFontAttributeName?


I have a NSComboBox and I want it to display the font name where the cursor is. I have the current code :

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

} 

Which sets the title of the combo box to the font but I get something similar to: "ArialMT 12.00 pt. P [] (0x1001a95b0) fobj=0x1006511c0, spc=3.33" And I just want to get Arial, or Times New Roman, or Helvetica, not that long string. How can I do this?


Solution

  • Use NSFont's displayName property.

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