Reputation: 1275
I am creating a UIButton
programically according the following mentioned code block. I need to assign RGB color to the text of the button. Its not applying. Any particular reason?
CGRect buttonFrame = CGRectMake(353, y, 607, 30);
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
button.tag = event.eventId;
[button setTitle:event.eventName forState: UIControlStateNormal];
button.titleLabel.font=[UIFont fontWithName:@"arial" size:14];
[button addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor colorWithRed:10.0 green:20.0 blue:100.0 alpha:1.0] forState: UIControlEventAllEvents];
[self.contentView addSubview:button];
Thanks
Upvotes: 2
Views: 2390
Reputation: 407
// no need to alloc UIButton..
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(353, y, 607, 30);
button.tag = event.eventId;
[button setTitle:event.eventName forState: UIControlStateNormal];
button.titleLabel.font=[UIFont fontWithName:@"arial" size:14];
[button addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor colorWithRed:10.0/255.0 green:20.0/255.0 blue:100.0/255.0 alpha:1.0] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[self.contentView addSubview:button];
Upvotes: 1
Reputation: 4656
According to Apple Documentation the color values range from 0.0 to 1.0, so You should divide the current values with their max possible value usually 255 like this:
[button setTitleColor:[UIColor colorWithRed:10.0/255.0 green:20.0/255.0 blue:100.0/255.0 alpha:1.0] forState: UIControlEventAllEvents];
Upvotes: 0
Reputation: 318774
RGB values need to be in the range 0.0 to 1.0. Divide each of your numbers by 255.0.
Upvotes: 3
Reputation: 4733
here is your reason :
write
[button setTitleColor:[UIColor colorWithRed:10.0/255.0 green:20.0/255.0 blue:100.0/255.0 alpha:1.0] forState: UIControlEventAllEvents];
Instead of just
[button setTitleColor:[UIColor colorWithRed:10.0 green:20.0 blue:100.0 alpha:1.0] forState: UIControlEventAllEvents];
Enjoy Programming..
Upvotes: 2