我正在根据下面提到的代码块以编程方式创建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];
由于
答案 0 :(得分:3)
RGB值必须在0.0到1.0的范围内。将每个数字除以255.0。
答案 1 :(得分: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];
享受编程..
答案 2 :(得分: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];
答案 3 :(得分: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];