如何为不同状态的 UIButton 设置相同的标题颜色?
How to set same title color of UIButton for different states?
我创建了一个 UIButton
的实例,并想为 Normal
状态、Selected
状态和 Highlighted
状态设置标题颜色。我这样做是因为-
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState: UIControlStateSelected];
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
使用上面的代码,当我构建和 运行 应用程序时,文本按预期显示为红色。
但是当我在一条语句中将所有状态的按钮标题颜色设置为-
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted];
构建并运行 应用...
问题
按钮上的文字显示为白色。
这(上面的说法)是不是设置UIButton
的标题颜色的正确方法,我需要在三个不同的语句中设置UIButton
的标题颜色吗?
如有任何建议/帮助,我们将不胜感激!
非常感谢。
只需为正常状态设置 titleColor
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
button.tintColor = [UIColor redColor];
你也可以用情节提要来做这些。
从状态配置属性,您可以select任何您想要的状态并相应地设置不同的属性。
UIControlState 定义为
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};
写的时候
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted];
这真的意味着你正在为状态 0x00000101 设置红色。按钮永远不会处于这种状态,因为它是未定义的。所以行为也是未定义的。
您需要了解位掩码的工作原理。 Merlin 指出了正确的方向,但他实际上并没有给出解释。
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};
UIControlStateNormal
是默认状态。它并没有真正使用位掩码。 ENUM
中的状态 UIControlStateHighlighted
、UIControlStateDisabled
、UIControlStateSelected
正在使用位掩码,因此可以按照您在 OP 中所做的方式使用。
例如,查看 ENUM
for UIUserNotificationType
:
typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) {
UIUserNotificationTypeNone = 0, // the application may not present any UI upon a notification being received
UIUserNotificationTypeBadge = 1 << 0, // the application may badge its icon upon a notification being received
UIUserNotificationTypeSound = 1 << 1, // the application may play a sound upon a notification being received
UIUserNotificationTypeAlert = 1 << 2, // the application may display an alert upon a notification being received
} NS_ENUM_AVAILABLE_IOS(8_0);
UIUserNotificationTypeNone
不是位掩码。您不会同时注册声音、徽章和 none 类型的通知。 None,其他的必须互斥(异或)。
同样适用于 UIControlState
。 UIControlStateNormal
不得与其他状态一起使用。您可以使用任意组合的位掩码值,但如果您只引入一个非位掩码值,结果将是您意想不到的。对于您的具体情况,Merlin 给出了将非位掩码值与其他位掩码一起使用的确切结果。
简而言之,如果您使用 UIControlStateNormal
,请同时设置 tintColor
。否则,仅使用位掩码值。
我创建了一个 UIButton
的实例,并想为 Normal
状态、Selected
状态和 Highlighted
状态设置标题颜色。我这样做是因为-
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState: UIControlStateSelected];
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
使用上面的代码,当我构建和 运行 应用程序时,文本按预期显示为红色。
但是当我在一条语句中将所有状态的按钮标题颜色设置为-
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted];
构建并运行 应用...
问题
按钮上的文字显示为白色。
这(上面的说法)是不是设置UIButton
的标题颜色的正确方法,我需要在三个不同的语句中设置UIButton
的标题颜色吗?
如有任何建议/帮助,我们将不胜感激!
非常感谢。
只需为正常状态设置 titleColor
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
button.tintColor = [UIColor redColor];
你也可以用情节提要来做这些。
从状态配置属性,您可以select任何您想要的状态并相应地设置不同的属性。
UIControlState 定义为
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};
写的时候
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted];
这真的意味着你正在为状态 0x00000101 设置红色。按钮永远不会处于这种状态,因为它是未定义的。所以行为也是未定义的。
您需要了解位掩码的工作原理。 Merlin 指出了正确的方向,但他实际上并没有给出解释。
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};
UIControlStateNormal
是默认状态。它并没有真正使用位掩码。 ENUM
中的状态 UIControlStateHighlighted
、UIControlStateDisabled
、UIControlStateSelected
正在使用位掩码,因此可以按照您在 OP 中所做的方式使用。
例如,查看 ENUM
for UIUserNotificationType
:
typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) {
UIUserNotificationTypeNone = 0, // the application may not present any UI upon a notification being received
UIUserNotificationTypeBadge = 1 << 0, // the application may badge its icon upon a notification being received
UIUserNotificationTypeSound = 1 << 1, // the application may play a sound upon a notification being received
UIUserNotificationTypeAlert = 1 << 2, // the application may display an alert upon a notification being received
} NS_ENUM_AVAILABLE_IOS(8_0);
UIUserNotificationTypeNone
不是位掩码。您不会同时注册声音、徽章和 none 类型的通知。 None,其他的必须互斥(异或)。
同样适用于 UIControlState
。 UIControlStateNormal
不得与其他状态一起使用。您可以使用任意组合的位掩码值,但如果您只引入一个非位掩码值,结果将是您意想不到的。对于您的具体情况,Merlin 给出了将非位掩码值与其他位掩码一起使用的确切结果。
简而言之,如果您使用 UIControlStateNormal
,请同时设置 tintColor
。否则,仅使用位掩码值。