43

I get a warning saying that setFont is deprecated?

[button setFont:[UIFont boldSystemFontOfSize:13]];

Any suggestions how to take it away pls..

Vladimir
  • 170,431
  • 36
  • 387
  • 313
SJ Reddy
  • 577
  • 1
  • 4
  • 13

3 Answers3

113

As UIButton exposes its titleLabel starting from iPhone OS 3.0 you must set font to it directly:

[button.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
Vladimir
  • 170,431
  • 36
  • 387
  • 313
11

The accepted answer works and sets the font for one button instance. In case you want to set application wide font for all UIButtons, you can do it like this:

// Set font to be used for labels inside UIButtons
[[UILabel appearanceWhenContainedIn:[UIButton class], nil] setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:15.0]];

This was not specifically asked in the question, but in case you need to set the font for all labels (not inside UIButtons), you can do it like this:

// Set font for all UILabels
[[UILabel appearance] setFont:[UIFont fontWithName:@"HelveticaNeue" size:13.0]];
lekksi
  • 4,868
  • 1
  • 16
  • 13
  • This is a good idea. sadly UILabel doesn't have the font property in the UIAppearance proxy and that's why the font doesn't work. See this answer http://stackoverflow.com/questions/17127921/appearance-proxy-not-working-as-intended-for-uibutton-font – Eva Madrazo May 07 '14 at 07:44
  • 1
    @EvaMadrazo could you elaborate what doesn't work? I'm using the code above in my project and it's working fine. To get `appearanceWhenContainedIn` working with `UIButton` set the button type to `UIButtonTypeCustom`. – lekksi May 07 '14 at 11:44
  • @EvaMadrazo oh, did you mean that the "Set font for all labels" doesn't work for `UIButton`s? That's true, I edited the answer to be more clear. – lekksi May 07 '14 at 11:50
  • Yes, uilabel does not include setFont in the appearance proxy. So, [[UILabel appearance] setFont:..] has no effect. – Eva Madrazo Jun 23 '14 at 11:54
  • @EvaMadrazo still, oddly enough i.e. [[UILabel appearance] setFont:[UIFont fontWithName:@"STHeitiJ-Light" size:15]]; seems to work for me just fine. – lekksi Jul 09 '14 at 10:06
11

Setting the font of the button directly is depracated in 3.x versions of the SDK. Instead, you need to set the properties of the button's titleLabel property.

Code: (mybutton).titleLabel.font = [UIFont systemFontOfSize:13];

Source: http://www.iphonedevsdk.com/forum/iphone-sdk-development/26126-warning-setting-font-button.html

Community
  • 1
  • 1
ceejayoz
  • 176,543
  • 40
  • 303
  • 368