PDA

View Full Version : Odd behaviour in 2.0 with a custom UILabel...




Aquis
Apr 26, 2009, 07:17 AM
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:

#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];

mainText = [t copy];

if (showFront == YES) {
self.text = t;
}
}

- (void)setAlternateText:(NSString *)t {
if (alternateText != nil) [alternateText release];

alternateText = [t copy];

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?



robbieduncan
Apr 26, 2009, 08:14 AM
Why are you synthesizing accessors and then providing your own accessor methods?

Aquis
Apr 26, 2009, 10:31 AM
Why are you synthesizing accessors and then providing your own accessor methods?

It's mainly for the getters; I haven't made them myself. Just the setters.

PhoneyDeveloper
Apr 26, 2009, 03:33 PM
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.

Aquis
Apr 26, 2009, 04:18 PM
Here's the differences in how it's rendered in the different versions.

iPhone OS 2.0:
http://towersnerd.com/images/robert/toggle_20.png

iPhone OS 2.1:
http://towersnerd.com/images/robert/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...

PhoneyDeveloper
Apr 26, 2009, 05:10 PM
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.

Aquis
Apr 27, 2009, 02:11 PM
This is basically how I create the label:

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!