UIButton - 触摸时保持文字黑色

时间:2011-09-10 00:08:59

标签: iphone ios uibutton

我有一个iPhone UIButton(自定义),它有背景图片和文字。

触摸时,图像变暗(良好)但文本从黑色变为白色。

如何使文字保持相同的黑色,以便在触摸按钮时,只有图像会改变颜色。

3 个答案:

答案 0 :(得分:12)

大多数情况下,以下行都会执行:

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

如果没有任何反应,请使用以下任一项:

[button setTitleColor:[UIColor blackColor] forState:(UIControlStateSelected | UIControlStateHighlighted | UIControlStateNormal)];

或者这可以解决问题:

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];

请参阅以下评论,原因是此答案已编辑/扩展,以包含2个单独的行。

答案 1 :(得分:5)

[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]

答案 2 :(得分:0)

okButton.titleLabel.textColor = [UIColor redColor];
[okButton addTarget:self action:@selector(isPressingForgetButton:) forControlEvents:UIControlEventTouchDown];
[okButton addTarget:self action:@selector(didPressForgetButton:) forControlEvents:UIControlEventTouchUpInside];


- (void) isPressingForgetButton:(id)sender
{
    UIButton * bt = (UIButton *)sender;
    bt.titleLabel.textColor = [UIColor greenColor];
}
- (void) didPressForgetButton:(id)sender
{
    UIButton * bt = (UIButton *)sender;
    bt.titleLabel.textColor = [UIColor redColor];
    [self gotoUnblockView];
}