Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton title doesn't show up

I'm having a little problem with a UIButton I try to create programmatically. The problem is that the button shows up (i.e. if I set a background color, I can clearly see the button) and is clickable, but the title label isn't visible.

Here's what I'm doing :

valueButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [valueButton addTarget:self action:@selector(openList:) forControlEvents:UIControlEventTouchUpInside];
    valueButton.frame = CGRectMake(160.0, decalageLabel+6.0, 120.0, 20.0);

    [valueButton.titleLabel setTextAlignment:UITextAlignmentRight];
    [valueButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Neue-Light" size:17.0]];
    UIColor * colorLabelValue = [[UIColor alloc] initWithRed:131.0/255.0 green:159.0/255.0 blue:86.0/255.0 alpha:1.0] ;

    [valueButton setTitleColor:colorLabelValue forState:UIControlStateNormal];

    if(txtValueField == NULL){
        txtValueField = @"";
    }
    valueButton.titleLabel.text = [NSString stringWithFormat:@"%@", txtValueField];
    NSLog(@"button : %@ ", valueButton.titleLabel.text);
    [self.contentView addSubview:valueButton];

Oh and I've made sure the title had an actual value, by NSLogging its text value, which works fine. So I don't know what's happening. Any idea?

like image 764
ChrisTauziet Avatar asked Mar 09 '26 18:03

ChrisTauziet


1 Answers

Don't use [[button titleLabel] setTitle:] for this. Use [button setTitle:@"blah" forControlState:UIControlStateNormal].

This allows you to set a different title for highlighted, selected, up, etc.

like image 123
Joshua Weinberg Avatar answered Mar 11 '26 07:03

Joshua Weinberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!