简体   繁体   中英

UIButton Default Title Color iOS7

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.

将颜色设置为nil,它的行为将返回到更改之前的状态,包括正确响应警报。

[myButton setTitleColor:nil forState:UIControlStateNormal];

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM