-->

appearance proxy not working as intended for UIBut

2019-09-07 02:56发布

问题:

im currently styling my app via the appearance proxy and i ran into this problem: when i set properties on the UIButton appearance my font is ignored:

[buttonAppearance setTitleColor:darkColor forState:UIControlStateNormal];
[buttonAppearance.titleLabel setFont:[UIFont fontWithName:@"Helvetica Neue" size:10.0]];

the first line is applied properly (darkColor is some UIColor), but my font change is ignored completely. When i copy the line into my ViewController and apply it to a concrete button it works fine.

Am i missing something?

any help appreciated! ty

回答1:

The font name is wrong, it should be HelveticaNeue, without the space between.

In the future if you want to see other iOS font names you should check this website piece of code

EDIT

After a closer look I realized that you are trying to set the appearance of the button's title which is a UILabel, sadly UILabel doesn't have the font property in the UIAppearance proxy and that's why the font doesn't work.



回答2:

I have found this class TWTButton.h, who resolved my problems adding a new appearance selector [setTitleFont:] to the UIButton class.

buttonAppearance = [TWTButton appearance];
[buttonAppearance setTitleFont:[UIFont systemFontOfSize:10.0f]];

You may read more about this here : http://toastmo.com/blog/2013/01/17/uiappearance/