Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Aquis

macrumors member
Original poster
Aug 7, 2007
63
0
Staffordshire, UK
Hello all, I have a UILabel subclass "UIToggleLabel", designed to toggle its value when the user taps on it, between two different values. For some reason, in 2.0, it appears in grey (I suspect it's only rendering a shadow for it), whilst in 2.1 and upwards, it renders fine.

Here's my class:
Code:
#import <UIKit/UIKit.h>


@interface UIToggleLabel : UILabel {
	NSString * mainText;
	NSString * alternateText;
	
	BOOL showFront;
}

- (void)toggle;

@property (nonatomic, copy) NSString * mainText;
@property (nonatomic, copy) NSString * alternateText;

@end

@implementation UIToggleLabel

@synthesize mainText;
@synthesize alternateText;

- (id)initWithFrame:(CGRect)frame {
	if ((self = [super initWithFrame:frame])) {
		showFront = YES;
		
		mainText = nil;
		alternateText = nil;
		
		self.userInteractionEnabled = YES;
	}
	
	return self;
}

- (void)setMainText:(NSString *)t {
	if (mainText != nil) [mainText release];
	
[B]	mainText = [t copy];[/B]
	
	if (showFront == YES) {
		self.text = t;
	}
}

- (void)setAlternateText:(NSString *)t {
	if (alternateText != nil) [alternateText release];
	
[B]	alternateText = [t copy];[/B]
	
	if (showFront == NO) {
		self.text = alternateText;
	}
}

- (void)toggle {
	showFront = !showFront;
	
	if (showFront) {
		self.text = self.mainText;
	} else {
		self.text = self.alternateText;
	}
	
	[self setNeedsDisplay];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	[self toggle];
}

- (BOOL)canBecomeFirstResponder {
	return YES;
}

- (void)dealloc {
	[mainText release];
	[alternateText release];
	
	[super dealloc];
}

@end

I've tracked down the problem to the bold lines; removing them (and passing through "t" straight to self.text), makes the text black again (but obviously without any toggling functionality).

I'd really like to continue compiling this for 2.0. Does anyone have any idea why this might be happening?
 
When you say it's grey do you mean that the text color is grey or something else?

I don't see how passing a string or a copy of that string should make a difference to anything.

You should check that t != mainText in your setter and do nothing if they're the same.
 
Here's the differences in how it's rendered in the different versions.

iPhone OS 2.0:
toggle_20.png


iPhone OS 2.1:
toggle_21.png


I don't see how passing a string or a copy of that string should make a difference to anything.
Neither do I! But, for some strange reason, that's what appears to be happening...
 
What font are you using and are you sure it's available on both OSes? Do you modify the text color in your code?

Are you testing on Sim or device? There is some issue where the fragile base class problem affects the sizes of objects and causes odd problems when building for one OS version and running on a later version, but only on Sim.
 
This is basically how I create the label:
Code:
		UIToggleLabel * versionLabel = [[[UIToggleLabel alloc] initWithFrame:CGRectMake(25,((img.size.height / img.size.width) * ([UIScreen mainScreen].applicationFrame.size.width - 50)) + 40 + titleLabel.frame.size.height,[UIScreen mainScreen].applicationFrame.size.width - 50,21)] autorelease];
		versionLabel.backgroundColor = [UIColor clearColor];
		versionLabel.textColor = [UIColor blackColor];
		versionLabel.mainText = [NSString stringWithFormat:NSLocalizedString(@"Version %1$@",nil),[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
		versionLabel.alternateText = [NSString stringWithFormat:NSLocalizedString(@"Build %1$@",nil),[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]];
		versionLabel.numberOfLines = 1;
		versionLabel.font = [UIFont italicSystemFontOfSize:[UIFont labelFontSize]];
		versionLabel.textAlignment = UITextAlignmentCenter;
		versionLabel.shadowColor = [UIColor whiteColor];
		versionLabel.shadowOffset = CGSizeMake(0,1);

Also, it's in the simulator. Haven't had chance to test this part on an actual device yet, I'll post back this weekend when I have the results!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.