tags:

views:

213

answers:

2

I have two UILabels. that I want to overlap one atop the other. Call the labels "under" and "over".

over: A C E G

under: B D F

UILabel "over" will have its text drawn in red. "under" will be in blue. The visual effect will be alternating colors between successive letters.

What are the controls available to me to exactly align the text in each label to pull this off?

Cheers, Doug

A: 

You have to make sure the font you are using is a monospace font, otherwise the characters will not line up exactly as you would hope with just a space between them. (I believe there is a typewrite font available in the iPhone OS that is monospace; YMMV.) Also you will have to prefix underLabel with a space for the code below to work.

To map one UILabel on top of another, try:

overLabel.opaque = NO; // so you can see what is under overLabel
overLabel.textColor = [UIColor redColor];
overLabel.backgroundColor = [UIColor clearColor];
underLabel.frame = overLabel.frame;
underLabel.textColor = [UIColor blueColor];

Note in the above code underLabel takes on overLabel's frame because the latter's frame is wider; if it were the other way around overLabel would get clipped.

All that being said I'd wager there is a better way to skin this particular cat. This solution feels very "round peg, square hole" to me.

fbrereto
A: 

I agree with fbrereton. This seems a very hard way to achieve the goal. Check out the NSString UIKit Additions to learn how to draw your own strings and lay the characters out yourself in your own custom -drawRect:. You'll have far greater control, and the code should not be that complex. iPhone doesn't have very good layout support (nothing like Mac offers), but for something this simple, it shouldn't be too bad.

Rob Napier
Cool. Thanks for the NSString UIKit pointer.
dugla