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

harry65

macrumors member
Original poster
Aug 26, 2008
36
0
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.
 
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:

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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.