如何将UILabel的textColor与其背景相匹配? [英] How to match UILabel's textColor to its background?

查看:63
本文介绍了如何将UILabel的textColor与其背景相匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一张背景图片,上面有一些UILabel。

背景图片可以由用户更改为任何颜色,而我必须确定哪种颜色会最好在UIL用于白色还是黑色?



最好的例子是iOS 7主屏幕。

当背景暗时,文本变为白色,反之亦然.b
但是,当背景介于黑暗和光线之间时,例如:半图像100%白色,另一半100%黑色 - 文本变为白色,并添加阴影效果,因此它将是可读。



我遇到的问题是如何指出这是什么类型的图像?
b $ b它是黑暗的吗?它轻吗?它是在暗光之间吗?

我怎样才能将此变为人类对代码的清晰度?


我想自己编写代码显然,但我没有任何线索。

我开始学习对比度和亮度,但我不能将各个部分粘在一起。

我应该从哪里开始?非常感谢你!


I have a background image and some UILabels above it.
The background image can be changed to anything by the user, while I have to determine which color would be better to use in the UILabels, white or black?

The best example for this is the iOS 7 Home Screen.
When the background is dark, the text becomes white, and vice versa.
But, when the background is between dark and light, for instance: half image 100% white and the other half 100% black - the text becomes white and drop shadow effects are added so it will be readable.

The problem that I've been facing is how to indicate what type of an image is this?
Is it dark? Is it light? Is it between dark-light?
How can I turn this, which is so clear to humans into code?

I want to write the code by myself obviously, but I have no clue.
I started learning about contrast and brightness, yet I can't glue the pieces together.
Where should I start? Thank you so much!

推荐答案

用这个 link

然后设置文字样式

CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
[averageColor getRed:&red green:&green blue:&blue alpha:&alpha];

int threshold = 105;
//Formula for computing Luminance out of R G B, which is something close to luminance = (red * 0.3) + (green * 0.6) + (blue * 0.1).
int bgDelta = ((red * 0.299) + (green * 0.587) + (blue * 0.114));

UIColor *textColor = (255 - bgDelta < threshold) ? [UIColor blackColor] : [UIColor whiteColor];

这样的事情。

你可以也可以使用上面的链接从图像中获取 UIColor 并使用亚光的类别,用于UIColor的光线或黑暗。

You could also use the link above to get the UIColor from the image and use matt's category for UIColor to get light or dark.

请看这个问题。肯定会帮助你。

PS:我正在复制代码,以便即使删除帖子,也可以获得信息

PS: I am Copying Code so that even if the post is deleted, information would be available

这篇关于如何将UILabel的textColor与其背景相匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆