简体   繁体   中英

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?

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM