I have a custom coloured background that the user can choose, this however means when they change the colour, the label is invisible because it is the same as the background and I do not want this. The code I have is: opacityLabel.textColor = bgView.backgroundColor;
In this bit of code the colour is the same at the start but doesn't change when I adjust the background colour, I also need the colour to continually change with the background colour, I was wondering what I could replace with "=" to make the label do the opposite colour. I can't do "!=" because that means is not equal to rather than opposite! If you can't do it this way could you provide me with a way of doing it?
-
1You shouldn't be aiming for an 'opposite color', since it does not exist. What you want is a color that is still readable given a background color, so contrast is more important.– Timothy GrooteCommented Jan 3, 2013 at 11:01
1 Answer
The first thing you'll need to do is calculate the opposite colour to the backgroundColor
, and then assign it to the textColor
property.
To do this you'll need to use the getRed:green:blue:alpha:
method on backgroundColor
. This will give you values between 0.0 and 1.0 for each component of your colour.
Then use the UIColor
class method colorWithRed:green:blue:alpha:
to create a new UIColor
, but use (1.0 - component value) for each of the components.
Then finally assign your new colour to opacityLabel.textColor