0

The objective is to change the font color and background color of my UITextView upon UIButton click and then revert them back to the original colors when the UIButton is clicked again; then repeat, ect.

Screenshot

Default Color Settings

  • Text: blackColor
  • Background: lightGrayColor

Upon Button Click (change to..)

  • Text: greenColor
  • Background: blackColor

(then if clicked again, change back to default color settings)

So far I have this:

- (IBAction) enableSleepMode:(id)sender {

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

}

I apologize but I'm not exactly sure where to go from here. Thanks in advance.

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
JDev
  • 5,168
  • 6
  • 40
  • 61
  • 1
    What exactly isn't working? That code seems like it would work to set it to green/black? You could do a `if (txtNotes.textColor == [UIColor greenColor])` to determine what state its in and swap back and forth... – Shizam Feb 15 '13 at 20:26

2 Answers2

1

try this

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

something like that is i think what you're looking for

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
  • Yes, that was exactly what I was looking for! Now how do I implement this because no matter where I add it, I return 2 errors: `Use of undeclared identifier setTextColor` and `Use of undeclared identified setBackgroundColor` Not that it is currently under my _enableSleepMode_ IBAction. – JDev Feb 15 '13 at 22:38
  • funny. did you rename button appropriately? I know it works fir title/bar. there is a more complex way of doing it. see http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uitextfield-uitextfielddelegate/. In their example, the background changes to green when the text field is active. it involves adding a boolean value. – Rachel Gallen Feb 16 '13 at 04:11
  • on the other hand see http://stackoverflow.com/questions/2270526/uisegmentedcontrol-selected-segment-color/5932524#5932524 – Rachel Gallen Feb 16 '13 at 04:18
1

You said to change the font color and background color of 'UITextView' not 'UIButton' Right? . Then did you add UItextViewDelegate?

I have done this like

EDIT:

    - (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";
    }
}
Midas
  • 930
  • 2
  • 8
  • 20
  • Ah! This is beautiful, thank you! I apologize if I did not make this clear in my original post. I'll try this out tonight when I get access to my work station. – JDev Feb 17 '13 at 16:24
  • it now says that `Property 'selected' not found on object UIBarButtonItem` http://content.screencast.com/users/revblaze/folders/Jing/media/a689747a-353b-4eb5-8c22-aab0e1fe842c/00000089.png – JDev Feb 17 '13 at 20:52
  • Note: I do have all sent actions and referencing outlets connected. – JDev Feb 17 '13 at 21:00
  • @MattBush You said its UIButton in the question. I don't think UIbarButton have selected property. You could do it with bar button like this . Check my edited code. Its very straight forward approach though. In the edited code 'str' is an NSString variable. Declare it in .h file – Midas Feb 18 '13 at 08:23
  • Worked beautifully. Thanks for all the help! – JDev Feb 18 '13 at 18:50