1

I wonder if it's possible to show live fonts in a popupbutton control (NSPopupButton)? currently, I load a popupbutton with a list of fonts available in the following manner.

NSArray *familyNames = [[NSFontManager sharedFontManager] availableFontFamilies];
NSMutableArray *fontarray = [[NSMutableArray alloc] initWithObjects:nil];
[fontarray addObject:@"- Select one - "];
for (NSString *family in familyNames) {
    [fontarray addObject:family];
}
[fontmenu1p addItemsWithTitles:fontarray];

Maybe, something like the following, using NSMutableAttributedString?

for (NSString *family in familyNames) {
    NSDictionary *attr1 = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:family size:[NSFont systemFontSize]],NSFontAttributeName,[NSColor blackColor],NSForegroundColorAttributeName,nil];
    NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:family];
    [aString setAttributes:attr1 range:NSMakeRange(0,family.length-1)];
    [fontarray addObject:aString];
}
[fontmenu1p addItemsWithTitles:fontarray];

I get an out of bounds error. I don't know if my approach is right. I don't even know if the popupbutton control supports styled text.

Thank you for your help.

El Tomato
  • 6,479
  • 6
  • 46
  • 75
  • Where did you get the out of bounds error? – gaige Jun 22 '13 at 08:52
  • I think it's the last line. I can't exactly tell since there are more than a hundred fonts to follow. Thanks. – El Tomato Jun 22 '13 at 13:38
  • My first thought was that fontarray could be empty. Actually, it's not. I get 348 if I count the number of items. In fact, if I display one of the objects with NSLog, I get the following. Arial Blac{ NSColor = "NSCalibratedWhiteColorSpace 0 1"; NSFont = "\"Arial-Black 13.00 pt. P [] (0x100528bc0) fobj=0x101b36150, spc=4.34\""; So I guess NSPopupButton cannot display styled text? Well, you can do it with VisualBasic/C#. So I thought I could do it with Objective-C. – El Tomato Jun 22 '13 at 19:38

1 Answers1

3

Even I did not tested it, I think, that our approach will not work. NSPopUpButton has a convenient API for its menu. Convenient, but short. (Typically pop-up items are not attributed, no separate views and so on.)

I would try to build an instance of NSMenuItem for each item. There is a setter -setAttributedTitle:, which lets you set attributed strings. Then you have to aggregate this to an instance of NSMenu and set the menu to the pop-up button.

BTW: [aString setAttributes:attr1 range:NSMakeRange(0,family.length-1)]; Why -1? The length is the length, not the index of the last char. And you want to set a range, which takes the length, not the index of the last character, too.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • Umm... I first set the size to family.length. After seeing an error, I changed it to family.length - 1. I wasn't sure. Thanks for pointing that out. – El Tomato Jun 22 '13 at 17:28
  • Of course: You had an index problem and it is the first thing to do to reduce ranges. But I do not think that this is the problem. – Amin Negm-Awad Jun 22 '13 at 18:06