UIBarButtonItem Font

I believe the following line should set the font size to 12 for all the control states, but font size goes back to default size for highlighted state.

[barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : [UIFont boldSystemFontOfSize:12.0] } forState:UIControlStateNormal];


I need to set the same font size explicitly for the highlighted state. Logical OR(UIControlStateNormal | UIControlStateHighlighted) is also not working.


Is there a way to set the same font size for all the control states in one online.

Replies

Can you post the exact complete code where you change the font ?

- (void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];

[barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : [UIFont boldSystemFontOfSize:12.0] } forState:UIControlStateNormal];

}

Did you try doing it in viewDidAppear instead of viewWillAppear ?

Try:


[barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : 
                [UIFont boldSystemFontOfSize:12.0] } 
                 forState:UIControlStateHighlighted];


Your goal of

"Is there a way to set the same font size for all the control states in one online."

is misplaced. Focus on readable, well organized code. Leave one line confusing commands behind you.

The answer is NO.


The parameter for `forState:` is just a bit pattern.


UIControlStateNormal 000

UIControlStateHighlighted 001


So, Logical OR(UIControlStateNormal | UIControlStateHighlighted) just generates 001, exactly the same bit pattern as UIControlStateHighlighted.


You may need to repeat calling the same method:

    UIFont *barButtonFont = [UIFont boldSystemFontOfSize:12.0];
    [barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : barButtonFont } forState:UIControlStateNormal];
    [barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : barButtonFont } forState:UIControlStateHighlighted];


And, this may not be an issue in your case, but UIControl may be in some other states.

UIControlStateDisabled 010

UIControlStateSelected 100


So, the bit pattern 011 represents the state `hilighted and disabled`, which is represented in Objective-C as `UIControlStateHighlighted | UIControlStateDisabled`. Which UIControl distinguishes from simple UIControlStateHighlighted.


So, if you utilize the control state disabled and selected and set exactly the same font for all possible states,

you may need to write more lines...

    UIFont *barButtonFont = [UIFont boldSystemFontOfSize:12.0];
    [barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : barButtonFont } forState:UIControlStateNormal];
    [barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : barButtonFont } forState:UIControlStateHighlighted];
    [barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : barButtonFont } forState:UIControlStateDisabled];
    [barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : barButtonFont } forState:UIControlStateHighlighted|UIControlStateDisabled];
    [barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : barButtonFont } forState:UIControlStateSelected];
    [barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : barButtonFont } forState:UIControlStateHighlighted|UIControlStateSelected];
    [barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : barButtonFont } forState:UIControlStateDisabled|UIControlStateSelected];
    [barButtonObj setTitleTextAttributes:@{ NSFontAttributeName : barButtonFont } forState:UIControlStateHighlighted|UIControlStateDisabled|UIControlStateSelected];

Ideally, the behavior defined for UIControlStateNormal is applicable to all the states. I see this issue only for UIBarButton(not for UIButton) control that too in iOS 11 and iOS 12. In iOS9, I don't have to set the same font for different control states explicitly, just setting font size 12 to UIControlStateNormal will set the same font size to all other control states as expected. That's why I asked whether or not anyone line code workaround is feasible.

Yes, I tried viewDidAppear, still no luck. Ideally, the behavior defined for UIControlStateNormal is applicable to all the states. I see this issue only for UIBarButton(not for UIButton) control that too in iOS 11 and iOS 12. In iOS9, I don't have to set the same font for different control states explicitly, just setting font size 12 to UIControlStateNormal will set the same font size to all other control states as expected.