Calculate text color based on iOS background color

I have these views by which I add UILabels as text, The Views can have a background color that is dynamic and cannot be selected,

Based on this, I need to find out the font color from only three options: two black or white or dark gray.

For example, on a dark red background, a white font would be more suitable than black, and on a light yellow, perhaps dark gray or black would be readable,

Is there any index that I could check against the background of UIColor to see the illumination?

+8
ios iphone uicolor
source share
1 answer

Take a look at my UIColor category and the blackOrWhiteContrastingColor method. This will tell you what will look better than black or white. You can expand it to maintain a dark gray color.

Basically, how it works, you need to look at the difference in brightness between the target color and black, and then between the target color and white. Then he selects the biggest difference in brightness.

+7
source share

All Articles