iOS UILabel使用负背景颜色作为文本颜色 [英] iOS UILabel use negative background color as text color

查看:111
本文介绍了iOS UILabel使用负背景颜色作为文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这项技术的任何流行词,所以我不幸在搜索引擎中找到了什么。

I don't know any buzz word for this technique, so I was not lucky to find something in search engines.

我想达到一个效果,这使得UILabel使用背景的负面颜色作为其文字颜色,(图片中的黑白就足够了)我的目的)。通过使用attributionText我可以为每个字母着色,但正如你在这张图片中看到的那样,我想要一种可以为单个字母带来不同颜色的效果。我最好的猜测是光栅化每个像素的标签和颜色,但我认为在性能方面有更好的方法。任何提示都将不胜感激。

I want to achieve an effect, that makes an UILabel use the nagative color of the background for its text color, (black and white like in the picture would be enough for my purpose). By using attributedText I could color every letter, but as you can see in this picture, I want an effect that can lead to different colors for a single letter. My best guess would be to rasterize the label and color every single pixel, but I assume there are better ways in terms of performance. Any hints would be appreciated.

推荐答案

如果我这样做,我可能会创建一个自定义 UIView 具有类似于 UILabel 的API(仅包含我需要的部分)以及指示应该反转文本部分的属性。

If I were to do this I would probably create a custom UIView class with an API similar to UILabel (with just the parts I needed) plus a property to indicate what portion of the text should be reversed.

然后我将实现 drawRect:方法,使用两个不同的剪切区域绘制文本两次。对于每一半,我会为文本和背景填充设置正确的颜色。

Then I would implement the drawRect: method to draw the text twice using two different clipping regions. For each half I would set the proper colors for the text and background fill.

这可以通过子类化 UILabel 附加一个属性加上自定义 drawRect:方法。

It's possible this could be done by subclassing UILabel with the one additional property plus the custom drawRect: method.

这篇关于iOS UILabel使用负背景颜色作为文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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