The following code is an example for a way you could customize a NSMenu in Objective C. It will add customized list items to your NSMenu by setting NSAttributedString objects for the title of the NSMenuItems instead of NSString objects.
//first you need a NSMutableDictionary to hold the attributes
self.folderItemFormatDictionary = [[NSMutableDictionary alloc]init];
/*NSAttributedString class has lot of attributes you can use to customize your list item
NSString *NSFontAttributeName;
NSString *NSParagraphStyleAttributeName;
NSString *NSForegroundColorAttributeName;
NSString *NSUnderlineStyleAttributeName;
NSString *NSSuperscriptAttributeName;
NSString *NSBackgroundColorAttributeName;
NSString *NSAttachmentAttributeName;
NSString *NSLigatureAttributeName;
NSString *NSBaselineOffsetAttributeName;
NSString *NSKernAttributeName;
NSString *NSLinkAttributeName;
NSString *NSStrokeWidthAttributeName;
NSString *NSStrokeColorAttributeName;
NSString *NSUnderlineColorAttributeName;
NSString *NSStrikethroughStyleAttributeName;
NSString *NSStrikethroughColorAttributeName;
NSString *NSShadowAttributeName;
NSString *NSObliquenessAttributeName;
NSString *NSExpansionAttributeName;
NSString *NSCursorAttributeName;
NSString *NSToolTipAttributeName;
NSString *NSMarkedClauseSegmentAttributeName;
NSString *NSWritingDirectionAttributeName;
*/
//In this example I will change the colour of the list item
[self.folderItemFormatDictionary setObject:[NSColor lightGrayColor] forKey:NSForegroundColorAttributeName];
//Using a for loop to add five list items
for (int i = 0; i < 5; i++) {
//Allocate and initialize a new NSMenuItem
NSMenuItem *item = [[[NSMenuItem alloc]init]autorelease];
//Use the function setAttributedTitle to set the title of the list item with the attributes you specified
[item setAttributedTitle:[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"folder %d",i] attributes:self.folderItemFormatDictionary]];
//add the new list item to the menu
[self.folderMenu addItem:item];
}
No comments:
Post a Comment