Sanjay Mohnani
Sanjay Mohnani

Reputation: 5967

How to set same title color of UIButton for different states?

I have created an instance of UIButton and want to set the title color for Normal state, Selected state and Highlighted state. And I am doing so as-

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

with above code when I build and run the app the text is shown up with red color as intended.

But when I set the title color of button for all states in one statement as -

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

build and run the app...

problem

the text on button is shown up in white color.

Is this(the above statement) not the correct way to set the title color of UIButton and does I need to set the title color of UIButton in three different statements?

Any suggestions/ help is appreciated!

Many thanks in advance.

Upvotes: 0

Views: 2258

Answers (4)

Schrodingrrr
Schrodingrrr

Reputation: 4271

You need to understand how bit masks work. Merlin has pointed in the right direction, but he hasn't actually given an explanation.

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 is a default state. It does not really use bit-mask. States UIControlStateHighlighted, UIControlStateDisabled, UIControlStateSelected in the ENUM are using bit-masks, so can be used in the way you have done in your OP.

For example, take a look at the 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 is not a bit mask. You don't register for notifications of types Sound, badge, and none at the same time. None, and others have to be exclusive (XOR of sorts).

Same applies to UIControlState. UIControlStateNormal must not be used with other states. You can use the bit-mask values in any combination, but if you introduce even a single non-bit-mask value, the results will be something you won't expect. For your specific case, Merlin has given the exact result of using a non-bit-mask value with other bit-masks.

In a nutshell, if you use UIControlStateNormal, set the tintColor along with it. Otherwise, use the bit-masked values only.

Upvotes: 1

Merlin
Merlin

Reputation: 213

UIControlState is defined as

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

When you write

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

it really means that you are setting red color for state 0x00000101. Button will never be in this state because it is undefined. So the behavior is undefined too.

Upvotes: 2

Suhail kalathil
Suhail kalathil

Reputation: 2693

You can do these with storyboard also.

button properties

From state config attribute , you can select any state you want and set different properties accordingly.

Upvotes: 0

Muhammad Adnan
Muhammad Adnan

Reputation: 2668

Just set titleColor for normal state

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

 button.tintColor = [UIColor redColor];

Upvotes: 2

Related Questions