2

我正在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];

谢谢

4

4 回答 4

3

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

于 2012-11-03T05:48:51.337 回答
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];

享受编程..

于 2012-11-03T05:48:36.323 回答
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];
于 2012-11-03T10:29:45.837 回答
0

根据Apple 文档,颜色值的范围从 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];
于 2012-11-03T07:39:35.560 回答