Search code examples
objective-cmacoscocoaalignmentnscombobox

Cocoa osx NSCombobox align items center


In my application for osx i have a NSCombobox which shows a list of items. My problem is that all of those items are always aligned to the left and I need that the last item stay center aligned. I know that in the interface builder user can set the alignment but, what about align only certain items? How can i achieve this correctly?

enter image description here

Sorry for my poor english. Thanks in advance.


Solution

  • Finally I solved like this:

    - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index
    {
    
    //RETURN THIS AT CONCRETE INDEX
    
    NSMutableAttributedString *text;
    text = [[NSMutableAttributedString alloc]initWithString:@myText;
    NSFont *font = [NSFont fontWithName:@"MyFont" size:16];
    [text addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, text.length)];
    
    [text addAttribute:NSForegroundColorAttributeName value:[NSColor blackColor] range:NSMakeRange(0, text.length)];
    
    //add alignment
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setAlignment:NSCenterTextAlignment];
    [text addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
    
    return text;  
    }