0

I want know programatically, my TextView has default backgroundColor or changed. for example:

if (myTextView.backgroundColor == defaultColor){
NSLog(@"default");
} else {
NSLog(@"changed");
}

I have one idea:

UITextView *etalon = [UITextVew new];
if (myTextView.backgroundColor == etalon.backgroundColor){
NSLog(@"default");
} else {
NSLog(@"changed");
}

But I think it's not quite right. Anybody have better ideas?

Alex
  • 2,100
  • 19
  • 26

6 Answers6

5

Try this -

const float* color1 = CGColorGetComponents(myTextView.backgroundColor.CGColor);
const float* color2 = CGColorGetComponents(etalon.backgroundColor.CGColor);

if(color1 == color2) {
    NSLog(@"Default");
} else {
     NSLog(@"Changed");
}
Akshay Nalawade
  • 1,447
  • 2
  • 14
  • 29
3

You should use [myTextView.backgroundColor isEqual:etalon.backgroundColor] in order to make a color compare. Also be careful because different color spaces are going to give you a not equal result.

D33pN16h7
  • 2,030
  • 16
  • 20
1

The property backgroundColor returns a UIColor object, with the color of you background. Just compare it with another UIColor.

Both of your options seems right, as long as etalon.backgroundColor and defaultColor are UIColor.

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
0

I don't know the context of your problem (ie. what are you trying to do), but another approach to your problem may involve using Key Value Observing to observe changes to your text view background color, and act accordingly.

Here's some documentation to get you started with KVO.

In code:

static void * kBackgroundColorCtx = &kBackgroundColorCtx;

[self.myTextView addObserver:self
                 forKeyPath:@"backgroundColor"
                    options:NSKeyValueObservingOptionOld
                    context:kBackgroundColorCtx];

Then, in your view controller implement this:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (context == kBackgroundColorCtx) {
        NSLog(@"Background color changed from %@ to %@", 
              [change objectForKey:NSKeyValueChangeOldKey],
              self.myTextView.backgroundColor);
    } else {
        // Not interested in this, pass to superclass
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
Daniel Martín
  • 7,815
  • 1
  • 29
  • 34
0

Check like this:

   if (myTextView.backgroundColor == [UIColor clearColor])
   {
       // your code here
   }
Gustavo Barbosa
  • 1,340
  • 1
  • 17
  • 27
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
0

How about using the appearance proxy to grab the default:

UITextView *appearanceProxy = (UITextView *)[UITextView appearance];
if ([myTextView.backgroundColor isEqualToColor:appearanceProxy.backgroundColor]){
    NSLog(@"default");
} else {
    NSLog(@"changed");
}

See samvermette's answer at How to compare UIColors? for the definition of isEqualToColor.

Community
  • 1
  • 1
Jonathan Arbogast
  • 9,620
  • 4
  • 35
  • 47