PopupMenu text color

I need to change the font color in a Popupmenu.

Any suggestions for OS X? (with or without declares).

You can do this by accessing the NSMenu of the popupMenu. You will need to set the attributedTitle of each NSMenuItem. Take a look at MacOSLib or MBS plugins. I can put together an example later if you need one.

An example would do fine. :wink:
You can use MBS Plugins if needed.

This will loop through the menu and set each item to be red. Using MacOSLib would be similar. NSColor has methods for creating custom RGB colors too. (NSColorMBS.colorWithDeviceRGB)

[code] dim m As new NSMenuMBS
m=me.NSPopUpButtonMBS.menu //get the cocoa menu

for i as integer=0 to m.numberOfItems-1

//first create a new NSMutableAttributedString with the menuitem text
dim attString As NSMutableAttributedStringMBS_
     =NSAttributedStringMBS.attributedStringWithString(m.Item(i)).mutableCopy

dim atts As new Dictionary           //to hold the attributes
dim r As new NSRangeMBS              //to set the range the attribute covers

r.Length=attString.length

//add a red foreground color attribute to the dictionary
atts.Value(NSAttributedStringMBS.NSForegroundColorAttributeName)=NSColorMBS.redColor 
attString.setAttributes(atts,r)   //set the attribute with the range

m.Item(i).attributedTitle=attString   //set the new title of the menuItem

next[/code]

Thanks. Works fine.

But it is a bit of ‘hack’. I mean, every time you change something in the popupmenu you have to use this. :slight_smile:

Xojo Inc should make a default Color property for this !!

BTW you need to change this:

dim attString As NSMutableAttributedStringMBS=NSAttributedStringMBS.attributedStringWithString(m.Item(i)).mutableCopy

to

dim attString As NSMutableAttributedStringMBS=NSAttributedStringMBS.attributedStringWithString(m.Item(i).Title).mutableCopy

Generally make the default framework behavior so

  1. by default apps follow the UI guidelines (which doesn’t usually include changing the color of the items in the popup menu)
  2. that where you want to do something nonstandard it is possible via declares

This is a good case of exactly that

[quote=115624:@Christoph De Vocht]But it is a bit of ‘hack’. I mean, every time you change something in the popupmenu you have to use this. :slight_smile:

[/quote]
You could subclass popupMenu with a new addRow method…

[code]Sub addrow(Text As String,textColor As color)
dim m As new NSMenuMBS
m=me.NSPopUpButtonMBS.menu //get the cocoa menu

//first create a new NSMutableAttributedString with the menuitem text
dim attString As NSMutableAttributedStringMBS_
= NSAttributedStringMBS.attributedStringWithString(text).mutableCopy

dim atts As new Dictionary //to hold the attributes
dim r As new NSRangeMBS //to set the range the attribute covers

r.Length=attString.length //color the entire string

//create an NSColor from the Xojo color
dim textNSColor As NSColorMBS_
=NSColorMBS.colorWithDeviceRGB(textColor.red/255,textColor.Green/255,textColor.Blue/255)

//create the new menuItem
dim item as NSMenuItemMBS=new NSMenuItemMBS
item.CreateMenuItem(text)

//add a foreground color attribute to the dictionary
atts.Value(NSAttributedStringMBS.NSForegroundColorAttributeName)=textNSColor
attString.setAttributes(atts,r) //set the attribute with the range

Item.attributedTitle=attString //set the new title of the menuItem
m.addItem item //add the item to the menu
End Sub
[/code]

Off course! Why didn’t I think if this. :slight_smile:

Thx again.

Would any one be kind enough to show Jim McKay’s example using Using MacOSLib. This is not something I am at all familiar with…