ios 突出显示时 UIButton 标题颜色更改 - 如何将其关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13030933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:54:38  来源:igfitidea点击:

UIButton title color change on highlight - How to turn it off?

iosuibuttontitletextcolor

提问by NewXcoder

I have created a button. The title's color is black by default. But when I press it, the color changes to be a little blue and never changes back again, how does this happen? Can anyone tell me why? And I want the button's title remain black all the time. How can I do that? I have tried

我创建了一个按钮。标题的颜色默认为黑色。但是当我按下它时,颜色会变成一点蓝色,再也不会变回来,这是怎么回事?谁能告诉我为什么?我希望按钮的标题始终保持黑色。我怎样才能做到这一点?我试过了

[button setTitleColor:[UIColor darkTextColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor darkTextColor] forState:UIControlStateSelected];

But There is no effect. When I add this in my code, it seems the button's title always blue.

但是没有效果。当我在代码中添加这个时,按钮的标题似乎总是蓝色的。

Code as follows.

代码如下。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(20, 360, 280, 44)];
[button setTitle:NSLocalizedString(@"Continue", @"Label: TextLabel in Continue button") forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
button.titleLabel.textColor = [UIColor darkTextColor];
button.titleLabel.shadowColor = [UIColor blackColor];
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth;

[self.view addSubview:button];
[button release];

Thanks everyone. I have sloved the problem. I think the root cause is

谢谢大家。我已经解决了这个问题。我认为根本原因是

button.titleLabel.textColor = [UIColor darkTextColor];

When I remove this, and use

当我删除这个,并使用

button setTitleColor:(UIColor) forState:(UIControlState);

The problem is solved!

问题已经解决了!

回答by PJR

you can use

您可以使用

[UIButton setTitleColor:forState:]

[UIButton setTitleColor:forState:]

for all the states , then title color will remain same for all states.

对于所有州,所有州的标题颜色将保持不变。

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

Note:To avoide type or paste above code three times you can use following code suggested by Will,

注意:为了避免输入或粘贴以上代码三次,您可以使用 Will 建议的以下代码,

[button setTitleColor:[UIColor redColor] forState:(UIControlStateHighlighted | UIControlStateNormal | UIControlStateSelected)];

回答by brandonscript

As @null points out, by far the simplest way to do this is to set the button type in Interface Builder (or in code) to "Custom".

正如@null 指出的那样,到目前为止,最简单的方法是将 Interface Builder(或代码)中的按钮类型设置为“自定义”。

If you need to replicate this behavior with a standard button, override the setHighlightedmethod to prevent the alpha channel of the titleLabel from adjusting too:

如果您需要使用标准按钮复制此行为,请覆盖该setHighlighted方法以防止 titleLabel 的 alpha 通道也被调整:

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    self.titleLabel.alpha = 1.0;
}

回答by SwiftArchitect

0 lines of code:

0行代码:

Using Interface Builder and either .XIBor .storyboard, select your UIButtonin IB:
View > Utilities > Show Attributes Inspector.

使用 Interface Builder 和.XIB.storyboardUIButton在 IB 中选择您的:
View > Utilities > Show Attributes Inspector

Select State Config(Default) to one of Highlighted, Selectedor Disabledand change the Text Colorattribute.

State Config(Default)选择为HighlightedSelectedDisabled 之一,然后更改Text Color属性。

Interface Builder solution

界面生成器解决方案

回答by Eric

There are a few comments pointing this out, but in order to have it as an actual answer:

有一些评论指出了这一点,但为了将其作为实际答案:

Set the button type to Customin your storyboard or in code: [UIButton buttonWithType:UIButtonTypeCustom];

将按钮类型设置为Custom在故事板或代码中: [UIButton buttonWithType:UIButtonTypeCustom];

回答by no_ripcord

watch out, system will ignore setTitleColor(_:for:) if button is not type custom.

注意,如果按钮不是自定义类型,系统将忽略 setTitleColor(_:for:)。

回答by Oliver Atkinson

For something a little more reusable you might consider this, as it doesnt violate the DRY principle. Add this as a category on UIButton.

对于更可重用的东西,您可能会考虑这一点,因为它不违反 DRY 原则。将此添加为 UIButton 上的类别。

- (void)oka_setTitleColor:(UIColor *)color forStates:(NSArray *)states;
{
  [states enumerateObjectsUsingBlock:^(NSNumber *state, NSUInteger idx, BOOL *stop) {
    [self setTitleColor:color forState:[state integerValue]];
  }];
}

example usage for your case:

您的案例的示例用法:

  [self oka_setTitleColor:[UIColor darkTextColor]
               forStates:@[@(UIControlStateNormal), @(UIControlStateHighlighted), @(UIControlStateSelected)]];

回答by Thomas Johannesmeyer

I think mayuur is right. Have you tried another color instead of "darkTextColor" though? As far as I know"darkTextColor" is a system specific color that is used to write Text on light backgrounds. Maybe try blackColor if mayuurs suggestion doesn't work.

我认为马尤尔是对的。您是否尝试过另一种颜色而不是“darkTextColor”?据我所知,“darkTextColor”是一种系统特定的颜色,用于在浅色背景上书写文本。如果 mayuurs 的建议不起作用,也许可以尝试 blackColor。

Edit: Try adding: [sender setHighlighted:NO];into your IBAction which is called on button press. Does it solve it? I suggest this because from the [button release];I guess you're still running an old version of the iOs SDK and there you don't have the option to disable the highlight of a button in an elegant way other than this.

编辑:尝试将:添加[sender setHighlighted:NO];到您的 IBAction 中,该操作在按下按钮时被调用。它解决了吗?我建议这样做是因为从[button release];我猜您仍然在运行旧版本的 iOs SDK,并且您无法选择以优雅的方式禁用按钮的高亮显示。

Edit2: You're creating the button programmatically but I don't see you connecting it with an IBAction. Add this below your [[UIButton alloc] init];

Edit2:您正在以编程方式创建按钮,但我没有看到您将其与 IBAction 连接。在你的下面添加这个[[UIButton alloc] init];

[button addTarget:self action:@selector(myIBAction) forControlEvents:UIControlEventTouchUpInside];

Then create an IBAction method like this:

然后像这样创建一个 IBAction 方法:

- (IBAction)myIBAction:(UIButton *)sender; /* In Header File */

- (IBAction)myIBAction:(UIButton *)sender{ /* In Implementation File */
        [sender setHighlighted:NO];
}