0

I have many different buttons in my app, yet most of them have the same properties assigned to them:

login = [[UIButton alloc]initWithFrame:CGRectMake(8, CGRectGetMaxY(password.frame) + 16, loginView.frame.size.width - 16, 40)];
[login setTitle:@"Login" forState:UIControlStateNormal];
[login.titleLabel setFont:[UIFont fontWithName:@"Avenir Next" size:18]];
[login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[login setTitleColor:[UIColor colorWithWhite:0.7 alpha:1] forState:UIControlStateHighlighted];
[login setTitleColor:[UIColor colorWithWhite:0.5 alpha:1] forState:UIControlStateDisabled];

Is there any way to create a class or something of a button that already has these default properties assigned? So I could simply go something like:

CustomButtom *btn = [CustomButton alloc]init];

Then the btn will have all of the above properties assigned?

Thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Josh
  • 745
  • 1
  • 7
  • 22

4 Answers4

2

Another way of handling this, is you can create a private method that will return the UIButton with the same properties. I think creating a subclass of UIButton is a little unnecessary.

cvu
  • 482
  • 2
  • 6
  • 20
0

You can do this by creating a CustomButton class

Xcode -> New File -> Cocoa Touch Class -> Next -> Name Your Button-> Select Subclass of UIButton

CustomButton.h file

#import <UIKit/UIKit.h>

@interface CustomButton : UIButton

@end

CustomButton.m file

#import "CustomButton.h"

@implementation CustomButton


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
//login = [[UIButton alloc]initWithFrame:CGRectMake(8, CGRectGetMaxY(password.frame) + 16, loginView.frame.size.width - 16, 40)];
[self setTitle:@"Login" forState:UIControlStateNormal];
[self.titleLabel setFont:[UIFont fontWithName:@"Avenir Next" size:18]];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithWhite:0.7 alpha:1] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor colorWithWhite:0.5 alpha:1] forState:UIControlStateDisabled];
}

@end 

Now, Call you button

CustomButton *customButton = [[CustomButton alloc]initWithFrame:CGRectMake(8, CGRectGetMaxY(password.frame) + 16, loginView.frame.size.width - 16, 40)];
[customButton addTarget:self action:@selector(loginButtonPressed:) forControlEvents:UIControlEventTouchDown];
[YourView addSubview:customButton];
Jamil
  • 2,977
  • 1
  • 13
  • 23
  • It's not advised to subclass UIButton: http://stackoverflow.com/questions/13202161/why-shouldnt-i-subclass-a-uibutton – chedabob Jan 03 '16 at 11:30
  • Also for the love of all that is holy, don't change the button state in `drawRect` unless you want your app to be unresponsive every time it draws the button – chedabob Jan 03 '16 at 11:30
0

You've got two options:

chedabob
  • 5,835
  • 2
  • 24
  • 44
-1

Yes. You can subclass the UIButton. When you override the init method setting the properties you could get the button's with the same properties.

Jason Nam
  • 2,011
  • 2
  • 13
  • 22