PDA

View Full Version : Move label on custom UIButton




harry65
Aug 10, 2009, 06:54 PM
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:


#import <Foundation/Foundation.h>

@interface MyButton : UIButton {

}

- (id)initWithText:(NSString *)text;
@end



#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 :o

Any help and/or ideas would be greatly appreciated.
Thanks.



amorya
Aug 10, 2009, 07:51 PM
try overriding layoutSubviews.

harry65
Aug 10, 2009, 08:21 PM
try overriding layoutSubviews.

Awesome!! Thanks so much for the help - that fixes it :D

spacetrader
Feb 6, 2010, 11:42 AM
Hi Harry65,

Can you post the code with the correction?

Thanks,

harry65
Feb 6, 2010, 11:56 PM
Hi Harry65,

Can you post the code with the correction?

Thanks,

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


#import <Foundation/Foundation.h>


@interface MyButton : UIButton {

}

- (id)initWithText:(NSString *)text;
@end



#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!

spacetrader
Feb 7, 2010, 02:39 PM
Harry,

Worked like a charm!!!:)