abhi1992 abhi1992 - 1 year ago 98
iOS Question

How to change title color of a set of UIButtons created programmatically?

In my app I have created a few buttons programmatically using a for loop as shown below.It is for a HORIZONTAL TAB MENU

In the action,I have to highlight the selected button(and greyout the remaining button titles).How to do this?
It should look almost like the image below.

When a button is clicked,the clicked button title color should be
white and all other buttons should have a grey color.I know I can
access sender.titlecolor in the button action.But What about the other

enter image description here

float buttonMinX=10;
float buttonMinY=0;
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _tabViewBackground.frame.size.width, _tabViewBackground.frame.size.height)];
scrollView.showsHorizontalScrollIndicator = NO;
[_tabViewBackground addSubview:scrollView];

for (int i=0; i<_tabItemsListArray.count; i++)
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tintColor=[UIColor whiteColor];
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 1;
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
UIFont *font1 = [UIFont fontWithName:@"HelveticaNeue-Thin" size:20.0f];
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
NSDictionary *dict1 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone),
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:[_tabItemsListArray objectAtIndex:i] attributes:dict1]];
[button setAttributedTitle:attString forState:UIControlStateNormal];

buttonWidth= [self getWidthOfRect:button.titleLabel];
button.frame = CGRectMake(buttonMinX,buttonMinY,buttonWidth,_tabViewBackground.frame.size.height);

sublayer = [[UIView alloc]init];
sublayer.backgroundColor = [UIColor greenColor];
sublayer.frame = CGRectMake(button.frame.origin.x,button.frame.size.height-2, button.frame.size.width,2);
[scrollView addSubview:sublayer];

if (ButtonIndex==i)

button.backgroundColor=[UIColor clearColor];
[scrollView addSubview:button];
scrollView.backgroundColor = [UIColor blueColor];

scrollView.contentSize = CGSizeMake(buttonMinX+10,_tabViewBackground.frame.size.height);

CGFloat widthIs =[titleLabel.text boundingRectWithSize:titleLabel.frame.size options:NSStringDrawingUsesDeviceMetrics attributes:@{ NSFontAttributeName:titleLabel.font }context:nil].size.width;
widthIs = ceilf(widthIs);
// NSLog(@"the width of yourLabel is %f", widthIs);
return widthIs+30;

- (void)action:(UIButton*)sender
for (int i=0; i<_tabItemsListArray.count; i++)
UIView *tabSelector = (UIView *)[self.view viewWithTag:kButtonSelectrorTag+i];
[tabSelector setHidden:YES];
UIView *tabSelector = (UIView *)[self.view viewWithTag:kButtonSelectrorTag+sender.tag];
[tabSelector setHidden:NO];

I have a button selector below every button.I should show one buttonSelector at a time.It is working great using the code in the

Answer Source

I've notice that you are using setAttributedTitle:forState:. You can set attributes for title in the same way but for UIControlStateSelected also.

Then, if you set button.selected = YES; attributes from UIControlStateSelected will apply. If you set button.selected = NO; attributes from UIControlStateNormal will apply.


You can create your buttons like this:

NSInteger numberOfButtons = 10;
NSMutableArray *menuButtonsMutableArray = [[NSMutableArray alloc] initWithCapacity:numberOfButtons];
for (int i = 0; i < numberOfButtons; i++) {
    UIButton *button = [UIButton new];
    //layout your button somehow
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(menuButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];

    [menuButtonsMutableArray addObject:button];

self.menuButtonsArray = [menuButtonsMutableArray copy];

Then in action method:

- (void)menuButtonDidTap:(UIButton *)sender {
    for (UIButton *button in self.menuButtonsArray) {
        button.selected = (button == sender);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download