簡體   English   中英

如何更改以編程方式創建的一組UIButton的標題顏色?

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

在我的應用中,我使用for循環編程方式創建了一些按鈕 ,如下所示。 用於水平選項卡菜單

操作中 ,我必須突出顯示選中的按鈕(其余的按鈕標題變灰)。如何執行此操作? 它看起來應該幾乎像下面的圖片。

單擊按鈕時,單擊的按鈕標題顏色應為白色,所有其他按鈕應為灰色。我知道我可以在按鈕操作中訪問sender.titlecolor。但是其他按鈕呢?

在此處輸入圖片說明

 -(void)createButtons
 {
     float buttonMinX=10;
     float  buttonMinY=0;
     scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _tabViewBackground.frame.size.width, _tabViewBackground.frame.size.height)];
     ButtonIndex=0;
     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.tag=i;
         [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),
                                    NSFontAttributeName:font1,
                                    NSParagraphStyleAttributeName:style};
         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);
         buttonMinX+=buttonWidth+05;

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

         sublayer.hidden=YES;
         if (ButtonIndex==i) 
         {
             sublayer.hidden=NO;
         }

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

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

-(CGFloat)getWidthOfRect:(UILabel*)titleLabel
{
    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];
}

我在每個按鈕下方都有一個按鈕選擇器,我應該一次顯示一個buttonSelector,使用action:的代碼效果很好action:

我注意到您正在使用setAttributedTitle:forState: 您可以通過相同的方式為title設置屬性,但也可以為UIControlStateSelected設置屬性。

然后,如果您設置button.selected = YES; UIControlStateSelected屬性將適用。 如果設置button.selected = NO; UIControlStateNormal屬性將適用。

編輯:

您可以這樣創建按鈕:

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];

然后在行動方法:

- (void)menuButtonDidTap:(UIButton *)sender {
    for (UIButton *button in self.menuButtonsArray) {
        button.selected = (button == sender);
    }
}
@IBOutlet var AllButtons: [UIButton]!
for button in AllButtons {
    if button.backgroundColor == UIColor.red {
        button.backgroundColor = compare.backgroundColor
    }
}

將所有按鈕拖放到AllButtons ,然后使用for循環訪問所有按鈕並執行您想做的任何事情。
這是一個簡單的示例,希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM