Default Font/Size Lost with NSAttributedString Without Declaring NSFontAttributeName?

Discussion in 'Mac Programming' started by Darkroom, Dec 12, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i've noticed that when adding attributes to a title (an NSMenuItem title in this case), if i don't specifically add an NSFontAttributeName to the attributes, the resulting attributed title will be much smaller and of different font than the other NSMenuItems.

    in the code below, i would like "Stop Clock" to remain the default size and font of an NSMenuItem, while changing the remainder of the string. the closest i can get is the commented out line below, but it's still not the correct font or size. is this normal? if so, what is the default name and size of NSMenuItems?

    Code:
    NSMutableAttributedString *timeRemaining = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Stop Clock - %d Remaining", [self timeMenuSelection]]];
    [timeRemaining addAttribute:NSForegroundColorAttributeName value:[NSColor grayColor] range:NSMakeRange(10, ([appendedTimeRemaining length]-10))];
    [COLOR="SeaGreen"]//[timeRemaining addAttribute:NSFontAttributeName value:[NSFont userFontOfSize:14] range:NSMakeRange(0, [appendedTimeRemaining length])];[/COLOR] 
    [MainMenu_Item3 setAttributedTitle:timeRemaining];
    
    perhaps it's possible to append the new attributed title to "Stop Clock" with something like the following? (something like meaning this code doesn't work).

    Code:
    [MainMenu_Item3 setAttributedTitle:[NSString stringWithFormat:@"Stop Clock %@", timeRemaining]];
    
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You could try a few things. First get the existing attributedTitle and make a mutableCopy of that and work with that. Second you could create a new one from scratch and get the font via [NSFont menuFontOfSize:]
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    thanks... menuFontOfSize:14 seems to refer to the default... i actually measured it in photoshop since it's not terribly obvious between fonts 14 and 15 on my screen. :eek:
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    [NSFont systemFontSize] might return that size, not sure.
     

Share This Page