2015-02-06 43 views
0

在我的應用程序中,我有一個NSCombobox顯示項目列表。 我的問題是,所有這些項目總是對齊到左側,我需要最後一項保持中心對齊。我知道在界面生成器中用戶可以設置對齊方式,但是,只對齊某些項目呢?我如何正確實現這一點?Cocoa osx NSCombobox對齊項目中心

enter image description here

對不起,我英文不好。提前致謝。

回答

0

最後我解決這樣的:

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

據我所知,它可能但不能通過IB。您需要以編程方式創建NSMenu併爲其添加視圖。這些視圖應該有中心對齊的標籤。

這比內存更重的一個簡單的菜單,所以我建議你不要,除非它不用於所有下拉菜單。

+0

您可以發佈任何例子或鏈接? – user3065901 2015-02-09 09:10:27