1
votes

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.

3

3 Answers

2
votes

When you get will popup notification and you can access the combo box window and navigate your desired item and change the alignment. Please refer this code https://github.com/ilg/IGResizableComboBox

1
votes

It is possible but not through IB as far as i know. You need to programmatically create an NSMenu and add views to it. These view should then have labels with center alignment.

This is heavier on memory than a simple menu so I suggest you don't unless it isn't used for all dropdowns.

1
votes

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