Mr.G
Mr.G

Reputation: 1275

UIButton text color with RGB

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

Answers (4)

Irshad Mansuri
Irshad Mansuri

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

Asif Mujteba
Asif Mujteba

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

rmaddy
rmaddy

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

Niru Mukund Shah
Niru Mukund Shah

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

Related Questions