iOS UILabel use negative background color as text color

和自甴很熟 提交于 2019-12-09 16:55:15

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/19917378/ios-uilabel-use-negative-background-color-as-text-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!