NSSegmentedControl how to set text and background color?

Hi
I’m trying to set the text color & the background color of a NSSegmentedControl, and getting no where fast
I have tried this, but it fails, am I supposed to target the cell instead?, any help with this appreciated

 segMentedSelection's setTitleTextAttributes:(NSColor's redColor)

cheers
Budgie

I wouldn’t be surprised if you can’t.

ah… one of those one’s aye, cheers

You’re on the right track, to set the text of the segments in an “NSSegmentedControl” you have to work with the backing “NSSegmentedCell”, which is a subclass of NSCell.
And you would use the setLabel:forSegment: function of either NSSegmentedControl or NSSegmentedCell, as the functions in NSSegmentedControl call the same functions in the NSSegmentedCell anyway, so you might as well directly access the NSCell.

So here’s a simple example that sets the label to the index of the segment.
And the “segmentedControlCell” property is an IB Outlet to the NSSegmentedCell in a xib file.


property segmentedControlCell : missing value

on applicationDidBecomeActive:notification
	if my segmentedControlCell's segmentCount() > 0 then
		repeat with index from 0 to ((my segmentedControlCell's segmentCount()) - 1)
			my (segmentedControlCell's setLabel:(index as text) forSegment:(index))
		end repeat
	else
		my (segmentedControlCell's setLabel:(0 as text) forSegment:(0))
	end if
end applicationDidBecomeActive:

The segments of the NSSegmentedCell are objects in a zero based array, and you can only set or get certain properties or attributes, but cannot access the segment object directly.
You access the segments by their index number, where the first leftmost one would have an index of 0.

The label of the segments is an NSString, and as Shane has concluded, it cannot be formatted or be an attributed string.
You can have an image as well or instead of a label string.
Like this.


property app : reference to current application

set segmentedControlImage to my app's NSImage's imageNamed:(my app's NSImageNameBonjour)
my segmentedControlCell's setImage:(segmentedControlImage) forSegment:(0)

I’ve used a standard system image for this example, but it could be any image you want to create.

The NSSegmentedControl is a fairly limited control when it comes to customisation.
The Back and Forward buttons in “Safari” are a NSSegmentedControl, with it’s segmentStyle: NSSegmentStyle property set to NSSegmentStyleSeparated.

So in conclusion, if you want to jazz up a “NSSegmentedControl” you will have to create some snazzy images for the segments.

Hope this helps a bit more.

Regards Mark

Thank you Mark
Great explanation, much better understanding of how this works now, it’s appreciated

Cheers