iOS UILabel use negative background color as text color

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

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.


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.

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.

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

链接地址: http://www.djcxy.com/p/17250.html

上一篇: 在iOS 7上延迟UIButton突出显示的状态

下一篇: iOS UILabel使用负面背景色作为文本颜色