UIButton Default Title Color iOS7

左心房为你撑大大i 提交于 2020-01-01 09:19:04

问题


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.


回答1:


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



回答2:


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.




回答3:


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




回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/18952064/uibutton-default-title-color-ios7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!