8

I want to know how to change the Title Colour of a UIButton back to the default value.

I have a button that I change the title colour to indicate a something is on, like this;

[cell.offStateSwitch setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

Then when it is off, I want to change it back to the default colour.

I cant figure out how to get the default system colour for a UIButton. I have found lots of people doing this by forcing a particular RGB value, but that is not necessarily correct across different versions of iOS.

Rohan Hamer
  • 81
  • 1
  • 1
  • 2

5 Answers5

13

Set the colour to nil and it's behaviour will return to how it was before you changed it, including responding properly to alerts.

[myButton setTitleColor:nil forState:UIControlStateNormal];
user2282219
  • 131
  • 1
  • 3
9

I assume you mean you want to set the text back to the tintColor. If you just want the current tintColor, you can do:

[button setTitleColor:button.tintColor forState:UIControlStateNormal];

However, the tintColor is supposed to automatically change in certain circumstances, such as when an alert pops up. In order to achieve that, I think you need to define your own button:

@interface MyButton : UIButton
@end

@implementation MyButton

- (void) tintColorDidChange
{
    [self setTitleColor:self.tintColor forState:UIControlStateNormal];
    [super tintColorDidChange];
}

@end

In that case, I propose you use the selected state for you special color:

[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

and set button.selected = YES when "something is on", as you say.

fishinear
  • 6,101
  • 3
  • 36
  • 84
4
button.tintColor = nil

or

[button setTintColor: null]

In iOS v7.0, all subclasses of UIView derive their behavior for tintColor from the base class. See the discussion of tintColor at the UIView level for more information.

This property has no default effect for buttons with type UIButtonTypeCustom. For custom buttons, you must implement any behavior related to tintColor yourself.

setting UIButton's tintColor to nil (null) will reset the color of its title

user3441734
  • 16,722
  • 2
  • 40
  • 59
1

There is no property for default title color in iOS, you can get it simply.

Just define defaultColor:

UIColor* defaultColor; then in your viewDidLoad put or wherever the view get initialized:

defaultColor = [button titleColorForState: UIControlStateNormal];

Then you have defaultColor as the default title color.

Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
0

Add the following code in cellForRowAtIndexPath delegate......

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

cell.button.tag=indexPath.row+100;

[cell.button addTarget:self selector:@selector(changeButtonTitle:) forControlEvents:UIcontrolTouchUpEventInside];

then in button action add the following code

-(void)changeButtonTitle:(UIButton *)button
{
    UITableViewCell *cell=(UITableViewCell *)button.superview.superview;
    NSIndexpath *indexpath=[self.yourtableview indexPathForCell:cell];
    UIButton *tempButton=(UIButton *)[cell viewWithtag:indexPath.row+100];
    [tempButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
DineshKumar
  • 1,641
  • 15
  • 13