inorganik
inorganik

Reputation: 25525

Set custom titleLabel properties for multiple UIButtons

I've got a custom button like:

UIButton *catBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
catBtn1.frame = CGRectMake(0, 344, 427, 67);
catBtn1.titleLabel.font = categoryFont;
catBtn1.titleLabel.textColor = [UIColor whiteColor];
catBtn1.titleLabel.frame = CGRectMake(52, 21, 150, 15);
catBtn1.titleLabel.textAlignment = UITextAlignmentLeft;

Is there a quick way to set all these titleLabel properties for all my buttons without doing this for each button? I tried assigning an instance of UILabel to catBtn1.titleLabel but that didn't work...

Upvotes: 0

Views: 119

Answers (2)

P R J
P R J

Reputation: 428

You can create method for button like this...

-(UIButton *) customBtn:(NSString *)btntxt:(CGFloat)btnx:(CGFloat)btny:(CGFloat)btnwidth
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //[btn setTitle:btntxt forState:UIControlStateNormal];
    btn.titleLabel.text = btntxt;
    btn.titleLabel.font = categoryFont;
    btn.titleLabel.textColor = [UIColor whiteColor];
    btn.titleLabel.frame = CGRectMake(52, 21, 150, 15);
    btn.titleLabel.textAlignment = UITextAlignmentLeft;
    [btn setFrame:CGRectMake(btnx, btny, btnwidth, 40)];
    return btn;
}

and you can call it like this any number of time when you need it

[YOURVIEW_OBJECT addSubview:[self customBtn:@"Samanmalliset" : 165:20 :115]];

Upvotes: 1

pronvit
pronvit

Reputation: 4279

You can create method that sets these properties and call it for each of your buttons.

Upvotes: 0

Related Questions