0

I'm trying to initially survey (log) all the fonts and font sizes I use in my app, fonts / sizes, that could be set programmatically or in nib files. Afterwards, I hope to use the code to set fonts too.

(I am aware of font type dynamics, however I'm have auto layout issues.)

I've got so far (see below), but ideally I'd like to just find anything with a font, I mean it could be a navigation bar not just buttons, labels or textfields.

Maybe I could somehow check to so see if the font method was a available ?

NSArray* buttonsArray = currentView.subviews;

for((UIButton*) button in buttonsArray)
{
     button.titleLabel.font = [UIFont fontWithName:@"myCustomFont" size:16];
}
Jules
  • 7,568
  • 14
  • 102
  • 186
  • Why not just use the 'Appearance' classes to set them? This wuold be safer and more defined. – Eiko Jun 19 '15 at 08:18
  • I'm not sure what you mean, I did a quick google, do you mean UIAppearance, perhaps you could point me at a url, to clarify your point? Thanks in advance/ – Jules Jun 19 '15 at 08:21
  • Please see my answer below – Eiko Jun 19 '15 at 08:32

4 Answers4

2

While asking if an object responds to font or setFont: is technically working, we now have the UIAppearance system, which are designed to give the app a consistent design. You can do things like "all my navigation bars should be green", or "all buttons should be blue, except those which are contained in .... which should be yellow". Many attributes are configureable, and setting this up is as easy as listing the desired appearance patterns for example in applicationDidLoad:.

http://nscookbook.com/2013/01/ios-programming-recipe-8-using-uiappearance-for-a-custom-look/ is one of the short introductions available on the net.

The good thing about using this instead of hacking something together while traversing your view tree is that it will work for all views, even those that you didn't create so far. If you set it manually, you have to do so after each view creation, which is tedious, wasteful and open to bugs. Also, you're less likely to break things that you didn't mean to, like third party classes.

Eiko
  • 25,601
  • 15
  • 56
  • 71
  • Question, so would I have to use dynamic type font properties, like headline and caption, to differentiate different sizes, could see that in that article, whats the best approach for different sizes? This does look like the way to go :) Thank. – Jules Jun 19 '15 at 08:41
  • No, you should be able to set "normal" fonts like you did before. :) – Eiko Jun 19 '15 at 08:46
  • So i'd have to remove code where i set fonts as this will override those? – Jules Jun 19 '15 at 09:22
  • Yes. The UIAppearance thing works on all views, and if you like, you can override them later. – Eiko Jun 19 '15 at 10:03
  • Just to be clear, when you say override, i would just set the font in the normal way? – Jules Jun 19 '15 at 12:11
  • Unfortunetly you can't use UIAppearance with UILabels http://stackoverflow.com/a/11839198/450456 – Jules Jun 20 '15 at 18:46
1

You can check if an object responds to -font by sending it the -respondsToSelector: message, like this:

if ( [ obj respondsToSelector:@selector( font ) ] )
{
    UIFont * font = [ (id)obj font ] ;
}
nielsbot
  • 15,922
  • 4
  • 48
  • 73
1

You can assign if the setter of the font is responding:

Note: code didn't tested

for(id obj in buttonsArray)
{
    if ( [obj respondsToSelector:@selector(setFont:)] )
    {
        // do something
    }
}
Eiko
  • 25,601
  • 15
  • 56
  • 71
EES
  • 1,584
  • 3
  • 20
  • 38
0
[obj respondsToSelector:@selector(font)];

if the method is retuen YES,then meant obj has font method.

skytoup
  • 126
  • 1
  • 2