在UIButton Press上更改现有的Set UIColors

时间:2013-02-15 20:00:40

标签: ios iphone uibutton uitextview

目标是在UIButton单击时更改我的UITextView的字体颜色和背景颜色,然后再次单击UIButton时将它们还原为原始颜色;然后重复,等等。

Screenshot

默认颜色设置

  • 文字:blackColor
  • 背景:lightGrayColor

按下按钮(更改为..)

  • 文字:greenColor
  • 背景:blackColor

(如果再次点击,则更改回默认颜色设置)

到目前为止,我有这个:

- (IBAction) enableSleepMode:(id)sender {

    [txtNotes setTextColor:[UIColor greenColor]];
    [txtNotes setBackgroundColor:[UIColor blackColor]];

}

我很抱歉,但我不确定从哪里开始。提前谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个

 (void)setTextColor:(UIColor *)color forState:(UIControlState)state

[button setTextColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTextColor:[UIColor blackColor] forState:UIControlStateHighlighted];

 (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state

[button setBackgroundColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor lightgrayColor] forState:UIControlStateHighlighted];
这样的事情就是我认为你在寻找什么

答案 1 :(得分:1)

您说要更改“UITextView”的字体颜色和背景颜色而不是“UIButton”对吗? 。然后你添加了UItextViewDelegate

我这样做了

修改

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    [txtView setBackgroundColor:[UIColor lightGrayColor]];
    [txtView setTextColor:[UIColor blackColor]];
    str = @"first";
    }

   -(IBAction)bt:(id)sender
{
    if([str isEqualToString:@"first"])
    {
        [txtView setBackgroundColor:[UIColor blackColor]];
        [txtView setTextColor:[UIColor greenColor]];
        str = @"second";
    }
    else
    {
       [txtView setBackgroundColor:[UIColor lightGrayColor]];
       [txtView setTextColor:[UIColor blackColor]];  
        str = @"first";
    }
}