1

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 Answers 3

2

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

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
  • can you post any example or link? Feb 9, 2015 at 9:10
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.