How do I set the text colour of the selected item in an iOSSegmentedControl?

Hey folks,

If you take a look at the sample project linked below, you can see that I’ve got an iOSSegmentedControl that’s sat on top of a dark canvas.

Using iOSControl.SetTintColor, I can set the colour that’s used to draw the outline and selected item background to white. However, the text colour of the selected item also stays white, and white-on-white is a little hard to read :wink:

Does anyone know how I can modify the text colour of the selected item?

Sample project

Sorry for the wrong reply – misread the question first.
According to https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/UISegmentedControl.html#//apple_ref/doc/uid/TP40012857-UISegmentedControl, tintColor is the way to change the text color too.
The only way I found to make the controls show anything was to send a partly transparent color: Try

SegmentedControl1.SetTintColor( &cFFFFFF33 )

in your open event.
Parts are not pure white then, but there is some contrast.

if you have access to BACKGROUND color then it, and TINTCOLOR are the only two colors that make a difference on a iOS SegmentedControl

the Selected segment will have a have a background of “tintcolor” and text of “background color”
while non selected segments will be the opposite… if that makes sense

// NOT XOJO CODE
let temp : UISegmentedControl = UISegmentedControl()
temp.frame=CGRectMake(61.0,113.0,200.0,28.0)
temp.backgroundColor=UIColor.redColor()
temp..tintColor=UIColor.orangeColor()
temp.insertSegmentWithTitle("Segment1", atIndex:0, animated:false)
temp.insertSegmentWithTitle("Segment2", atIndex:1, animated:false)
temp.selectedSegmentIndex=1

Thanks, Ulrich & Dave.

Ulrich: thanks for the transparency tip. It’s not ideal, but does at least make it possible to read the text.

Dave: we don’t have access to the backgroundColor property via Xojo, so this looks like something that can only be achieved via declares. I’ll look forward to Ulrich’s session on declares at the MBS conference in a couple of weeks’ time and put it to use sorting this out :wink:

I am afraid I’ll have to disappoint you in that case. The documentation says nowhere that a standard buttonCell would be used; it really only gives access to images and text properties. While it still could be that Apple uses cells behind the stage, they did not open access to them. So the only way I see currently would be a custom control.

perhaps setTitleTextAttributes:forState:
this isn’t exposed directly though

Of course! You could track down the view hierarchy and see if on UIResponder level one of the views is a textcell (or subytpe). Well, Tom, I don’t know if I’ll find time. Currently I don’t have much more than the headlines for the session. But I’ll see what I can do :smiley:

It does look like you should be able to set background color even if it’s only via a declare
The inheritance tree suggests that should work
I just have no idea what Apple will do with that at run time :stuck_out_tongue:

Should be no problem. A UISegmentedControl is still a UIControl is still a UIView is still a UIResponder so everything that works for these classes works for the lowest subclass too. And yes, it’s still a NSObject of course.

I haven’t found any documentation on the inner structure.

Tom, if you’re feeling adventurous, you might start iOSLib and create a subclass of AppleControl. Name it like AppleSegmentedControl and copy a classptr from another class, make sure its name is not extended with a number and change the string literal in it to “UISegmentedControl”.
Create a Shared method MakeFromPtr (aPtr As Ptr) As AppleSegmentedControl with the code

Return if (aPtr = nil, Nil, New AppleSegmentedControl (aPtr))

That should be enough to create an iOSLib object you can attach to an iOSSegmentedControl:

Dim ASC As New AppleSegmentedcontrol (mySegmentedControl.handle)

whose structure you could examine in the debugger or maybe copy properties from the AppleView class like its SubView AppleArray property for further debugger examination which maybe could give access to single cells if such exist, or simply play with the AppleView properties like backgroundColor or its layer.

EDIT: Hey, that could fill a few minutes of the session: “How do I create my own controls?” :smiley: Thanks!

Thanks, Ulrich. I’m away for a bit at the moment, but I’ll try and look at this before the conference.

I believe some things are possible:

Ulrich… do tell… I have not been able to skin a segemented control like that even in native Swift

Examine the subviews! The segmentedcontrol has an array of subviews (UISegment) that seems to be a spin-off of a normal UIView, starting with the rightmost. Each has two subviews again, one for the image, one for the text. Both no real subclasses of UIImageView or UITextField, but accessible as UIViews and the latter one accepts a setTextColor: too.
Should be no problem for Apple as long as you only use official UIView or available subclass’ features, I guess. But cannot guarantee …

Sorry, Dave, correction:
The first subview of a UISegment is a standard UIImageView, and the second one is a UISegmentLabel which doesn’t claim to inherit from anywhere but as the name suggests understands all the features of an UILabel.