0

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

截屏

默认颜色设置

  • 文本:blackColor
  • 背景:lightGrayColor

单击按钮后(更改为..)

  • 文本:greenColor
  • 背景:blackColor

(然后如果再次单击,更改回默认颜色设置)

到目前为止,我有这个:

- (IBAction) enableSleepMode:(id)sender {

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

}

我很抱歉,但我不确定从这里去哪里。提前致谢。

4

2 回答 2

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

类似的东西是我想你在找什么

于 2013-02-15T20:32:51.630 回答
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";
    }
}
于 2013-02-16T06:48:59.360 回答