Florik Florik - 4 months ago 14x
iOS Question

Inverse UIlabel text color when its frame intersects an UIView

Hi Im trying to change the text color of a UILabel in the case when the labels rect intersects an UIView, i was thinking maybe there is a property or something i can use. Like in the image below:

enter image description here

Did someone meet this issue before, or what should i read to start doing this thing ?

Thanks in advance


Good puzzle! Here's what I would do:

  1. Two UIViews. Let's call one the background and the other the progressBar. progressBar is stacked on top of background with the same origin on their common superview.
  2. They both have a UILabel as subview, and both labels at the same origin relative to their parent. background has a dark backgroundColor and it's label has light textColor and the progress view has things the other way around.
  3. progressBar has a narrower frame width than background and has clipsToBounds==YES

The trick is, with the views' origins the same and the labels' origins the same, and clipsToBounds on the top view, everything is going to look right.

Drop those two views into a new UIView subclass called ReallyCoolProgressView, and give it one public method:

-(void)setProgress:(float)progress progress is a number from 0.0 to 1.0. The method scales the progressBar width and sets both label's text @"Progress %f", progress*100