Odd behaviour in 2.0 with a custom UILabel...

Discussion in 'iOS Programming' started by Aquis, Apr 26, 2009.

  1. macrumors member

    Joined:
    Aug 7, 2007
    Location:
    Staffordshire, UK
    #1
    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?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Why are you synthesizing accessors and then providing your own accessor methods?
     
  3. thread starter macrumors member

    Joined:
    Aug 7, 2007
    Location:
    Staffordshire, UK
    #3
    It's mainly for the getters; I haven't made them myself. Just the setters.
     
  4. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    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.
     
  5. thread starter macrumors member

    Joined:
    Aug 7, 2007
    Location:
    Staffordshire, UK
    #5
    Here's the differences in how it's rendered in the different versions.

    iPhone OS 2.0:
    [​IMG]

    iPhone OS 2.1:
    [​IMG]

    Neither do I! But, for some strange reason, that's what appears to be happening...
     
  6. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    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.
     
  7. thread starter macrumors member

    Joined:
    Aug 7, 2007
    Location:
    Staffordshire, UK
    #7
    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!
     

Share This Page