0

I have to implement NSMenuItem such a way that selected NSMenuItem should have Bold Text , this is what i have done,

@implementation NSMenuItem (Font)

-(void)setBoldStyle:(bool)bBold{
    NSString* title = [self title] ;

    NSFont *pFont = (bold)?[NSFont boldSystemFontOfSize:14]:[NSFont menuFontOfSize:12];

    NSDictionary* fontAttribute = [NSDictionary dictionaryWithObjectsAndKeys:
                                   pFont, NSFontAttributeName,
                                   nil] ;

    NSMutableAttributedString* newTitle = [[NSMutableAttributedString alloc] initWithString:title
                                                                                 attributes:fontAttribute] ;

    [self setAttributedTitle:newTitle] ;
    [newTitle release] ;

}

@end

With Above peiece of code, i am able to set the bold text when a particular NSMenuItem gets selected, but if it needs to be toggled ( Means if an item was bold earlier, it should be normal now), then its not happening,

This is the way i am calling it,

    // have we selected any menuitem yet
    if ( prevStatusIndex >0){
        // then deselect it
        pTempMenuItem = [pMenu itemAtIndex:prevStatusIndex];
        [pTempMenuItem setBoldStyle:NO];
    }

    prevStatusIndex = clientStatus+1;
    pTempMenuItem = [pMenu itemAtIndex:prevStatusIndex]; // 1 because a separator added
    [pTempMenuItem setBoldStyle:YES];

Any idea whats going wrong ?

Amitg2k12
  • 3,765
  • 10
  • 48
  • 97
  • It looks like you are trying to have just one item selected from a group - select an item and the previous selection should be deselected. At a glance your `setBoldStyle` looks OK, but the second block of code does not show enough. The logic looks about right - if there was a previous selection deselect it, then select the new selection. Are you sure the `if` is evaluating to `YES` when there was a previous selection? HTH – CRD Apr 05 '13 at 19:11
  • @CRD : I checked it :( , if i put some log, it looks its using correct NSMenuItem pointer – Amitg2k12 Apr 06 '13 at 11:10
  • Have you set a breakpoint on setBoldStyle? Check bold (I assume bBold is a typo above - unless it is not and you also have an instance var bold...), step through, does the right font get selected? Etc. – CRD Apr 06 '13 at 20:33
  • i did it first before posting it to SO :( – Amitg2k12 Apr 08 '13 at 05:57

2 Answers2

0

You need to use similar to this :

if ([pTempMenuItem boldStyle]) {
    NSLog(@"currently bold. change it");
    [pTempMenuItem setBoldStyle:NO]);
}
else{
    [pTempMenuItem setBoldStyle:YES]);
    NSLog(@"currenlty normal. change it");
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

We can only guess as there is a lot of information missing from your question - where are prevStatusIndex, pMenuItem, pMenu and clientStatus declared and given values? What is the valid range of clientStatus? Etc.

In the comments you've said you have used the debugger and breakpoints, but gave no indication of what values you saw.

You really need to provide more detail so folk can help you.

Provided the selected index is never 0 (i.e. prevStatusIndex is not 0 or clientStatus is not -1) and pMenu points to the correct menu then your code works. If the selected index can be zero then you need to change your test for de-bolding to prevStatusIndex >= 0 otherwise the first entry in the menu can be bolded but not unbolded.

HTH.

CRD
  • 52,522
  • 5
  • 70
  • 86