2012-11-03 72 views
2

我正在创建一个UIButton根据下面提到的代码块编程。我需要将RGB颜色分配给按钮的文本。它不适用。任何特定的原因?UIButton文本颜色与RGB

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

感谢

回答

2

这里是你的原因:

[button setTitleColor:[UIColor colorWithRed:10.0/255.0 green:20.0/255.0 blue:100.0/255.0 alpha:1.0] forState: UIControlEventAllEvents]; 

而不是只是

[button setTitleColor:[UIColor colorWithRed:10.0 green:20.0 blue:100.0 alpha:1.0] forState: UIControlEventAllEvents];

Enjoy Programming ..

3

RGB值需要在0.0到1.0的范围内。将每个数字除以255.0。

0

根据Apple Documentation颜色值的范围从0.0到1.0,所以应该用它们的最大可能的值通常255像这样将当前值:

[button setTitleColor:[UIColor colorWithRed:10.0/255.0 green:20.0/255.0 blue:100.0/255.0 alpha:1.0] forState: UIControlEventAllEvents]; 
1
// 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];