Move label on custom UIButton

Discussion in 'iOS Programming' started by harry65, Aug 10, 2009.

  1. harry65 macrumors member

    Joined:
    Aug 26, 2008
    #1
    Hi All -

    I'm hoping someone out there can help me with this... I have a subclass of a UIButton called MyButton. I want to move the title of the button to the top-left corner of the button. An example of what I'm doing looks like this:

    Code:
    #import <Foundation/Foundation.h>
    
    @interface MyButton : UIButton {
    
    }
    
    - (id)initWithText:(NSString *)text;
    @end
    
    Code:
    #import "MyButton.h"
    
    
    @implementation MyButton
    
    - (id)initWithText:(NSString *)text
    {
    	self = [MyButton buttonWithType:UIButtonTypeCustom];
    	[self setTitle:text forState:UIControlStateNormal];
    	[self setBackgroundColor:[UIColor redColor]];
    	[self setFrame:CGRectMake(0, 0, 100, 100)];
    
    	UILabel *titleLabel = [self titleLabel];
    	CGRect fr = [titleLabel frame];
    	fr.origin.x = 5;
    	fr.origin.y = 5;
    	[[self titleLabel] setFrame:fr];
    	
            // HACK
    	[self addTarget:self action:@selector (clicked) forControlEvents:UIControlEventTouchUpInside];
    	
    	return self;
    }
    
    - (void)clicked
    {
    	NSLog (@"Clicked");
    	UILabel *titleLabel = [self titleLabel];
    	CGRect fr = [titleLabel frame];
    	fr.origin.x = 5;
    	fr.origin.y = 5;
    	[[self titleLabel] setFrame:fr];
    
            // HACK 2
    	[self setNeedsDisplay];
    }
    
    - (void)drawRect:(CGRect)rect
    {
    	NSLog (@"Draw rect");
    	UILabel *titleLabel = [self titleLabel];
    	CGRect fr = [titleLabel frame];
    	fr.origin.x = 5;
    	fr.origin.y = 5;
    	[[self titleLabel] setFrame:fr];
    }
    
    @end
    
    This works as expected; when my button is drawn the label is in the top-left corner. However, whenever I click the button the label goes back to the center of the button. I tried to put in a hack where, when clicked, I would re-position the label again. When I try that, the label goes to the center when clicked, then goes to the top-left, then goes to the center again.

    I even tried re-positioning the title in the drawRect method, and the same thing happens where the title goes to the top-left for a spit-second and then back to the center.

    I'm not really sure what's going on here. I'd like to be able to position the title label just once (and not use the 'clicked' hack), but getting it to work at all would be nice too :eek:

    Any help and/or ideas would be greatly appreciated.
    Thanks.
     
  2. amorya macrumors 6502

    Joined:
    Jun 17, 2007
  3. harry65 thread starter macrumors member

    Joined:
    Aug 26, 2008
    #3
    Awesome!! Thanks so much for the help - that fixes it :D
     
  4. spacetrader macrumors newbie

    Joined:
    Feb 6, 2010
    #4
    Post Code, Please

    Hi Harry65,

    Can you post the code with the correction?

    Thanks,
     
  5. harry65 thread starter macrumors member

    Joined:
    Aug 26, 2008
    #5
    Basically, I took all of the positioning code out of the constructor and put it in layoutSubviews. Here's what I ended up doing:

    Code:
    #import <Foundation/Foundation.h>
    
    
    @interface MyButton : UIButton {
    
    }
    
    - (id)initWithText:(NSString *)text;
    @end
    
    Code:
    #import "MyButton.h"
    
    
    @implementation MyButton
    
    - (id)initWithText:(NSString *)text
    {
    	self = [MyButton buttonWithType:UIButtonTypeCustom];
    	[self setTitle:text forState:UIControlStateNormal];
    	[self setBackgroundColor:[UIColor redColor]];
    	[self setFrame:CGRectMake(0, 0, 100, 100)];	
            return self;
    }
    
    - (void)layoutSubviews
    {
    	[super layoutSubviews];
    	NSLog (@"Layout subview");
    	
    	UILabel *titleLabel = [self titleLabel];
    	CGRect fr = [titleLabel frame];
    	fr.origin.x = 5;
    	fr.origin.y = 5;
    	[[self titleLabel] setFrame:fr];
    }
    
    @end
    
    Hope that help!
     
  6. spacetrader macrumors newbie

    Joined:
    Feb 6, 2010

Share This Page