简体   繁体   中英

Negative text color with respect to background image

I'm trying to subclass (because thats probably the easiest way to do it) UILabel so it's text color is negative to the part of the background image it overlays. I also have an approximate idea how to do that, but cannot transform it into code.

图1

Parameters to pass the subclass

  • Background image
  • Position of the UILabel within the background image (probably CGRect as it needs to know the x, y, w, h of the background image the UILabel will cover) - this could also be just the UILabel's rect, as the UILabel will be the subview of an UIImageView containing the background image.

Logic while drawing the label

  1. Create a mask with the text - the content of the mask would only be the text itself, not the background.
  2. Crop out the specific part (rect) of the Background image which is overlaid by the label.
  3. Delete (or make transparent) all of the image outside the text mask -> that way you would only be left with the image where the text is, the rest would be gone.
  4. Invert the image's colors (with this method probably) or use kCGBlendModeDifference in some way perhaps?
  5. Draw all that in one of the methods UILabel's drawRect or drawTextInRect methods.

Usage

The usage of this would probably be something like

  1. Alloc an UIImage
  2. Display it in an UIImageView
  3. Create the UILabel subclass with the inverted text color relative to the part of the image the UILabel will be placed in
  4. Add the UILabel as a subview of the UIImageView

Issues this might have

I can't approximate how quick this will be while scrolling, however this could be solved by drawing the text right into the UIImage , as it will be static. If the UIImageView holding the label would be put into a UIScrollView , would it be redrawn upon every scroll?

Thank you a lot in advance!

Check out the answer for my question: drawRect drawing 'transparent' text?

There's a number of answer including a great open-source project you can use from Robin Senior.

Enjoy!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM