1

I have added button in interface builder and referenced them, then I want to change their title font, but it doest work. When I use:

[_button setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:24.0f]];

it WORKS, but this code is depreciated. So I have looked online to see what to use instead of this code, I found this code to use:

[_button.titleLabel setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:24.0f]];
_button.titleLabel.font = [UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:24.0f];

but NEITHER of this work. I have no idea what to do with it or what is wrong. I have tried to set globally appearance of UIButton, that works too, but I cant use it as I need that font only on some buttons.

Button is as:

@property (nonatomic, weak) IBOutlet UIButton *button;

and added in storyboard.

Font is defined:

#define APP_FONT_FUTURASTD_LIGHT @"FuturaStd-Light"

and added to Info.plist section "Fonts provided by application".

Tomáš Chylý
  • 342
  • 1
  • 8
  • 21
  • This answer seems to suggest your alternative code should work fine: http://stackoverflow.com/questions/4206848/setfont-deprecated – trojanfoe Aug 29 '13 at 08:19
  • I saw that question, tried it, but it doesnt do anything. It doesnt show warning/error and neither does it change the font. Thats why Im confused. – Tomáš Chylý Aug 29 '13 at 08:25
  • 1
    OK the next thing to check is for `nil` of the button or font... – trojanfoe Aug 29 '13 at 08:25
  • That is OK too, both button and font are what they are supposed to be, NSLog shows them and when I change something else it works. Btw I use this to set button, is that ok? @property (nonatomic, weak) IBOutlet UIButton *button; – Tomáš Chylý Aug 29 '13 at 08:35
  • Yeah that looks OK to me. Not sure what is wrong here. – trojanfoe Aug 29 '13 at 08:36
  • It looks like either UIButton or the UIFont object is nil. verify the typo in APP_FONT_FUTURASTD_LIGHT. Apart from that your code should work. – Prasad Devadiga Aug 29 '13 at 08:42
  • UIButton and UIFont are ok, also the typo is ok. As I wrote if I use the depreciated code it changes Font to what I want, but when I replace it with the alternate code, it doesnt do anything. Also checked titlelabel if its nil and it is ok too. – Tomáš Chylý Aug 29 '13 at 08:56
  • Add the code of button initialization and adding the font, here in question – Lithu T.V Aug 29 '13 at 09:08
  • I guess the font name you mentioned is not a native font (I found `Futura` in interface builder, but `FuturaStd-Light`) you can either try replacing ``FuturaStd-Light` with `Futura` or download `FuturaStd-Light` font and set it as a custom font. [Check this](http://stackoverflow.com/questions/12633744/tamil-font-in-ios/12633893#12633893) – Ankur Arya Aug 29 '13 at 12:18
  • It is custom font and as I wrote it is added and works on labels, textviews, etc. properly. – Tomáš Chylý Aug 29 '13 at 12:28
  • a work around is put a `UILabel` on you button and set text to that `UILabel` – Ankur Arya Aug 29 '13 at 12:35
  • yes, I did some buttons like that, but it is more time cunsuming to do all buttons like that – Tomáš Chylý Sep 03 '13 at 07:27

3 Answers3

3

I have found out what the problem was. The other programmer who has started this project has set

[[UIButton appearance] setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_BOLD size:12.0f]];

in CSAppDelegate.m and so all my customizations were overwritten by this.

So to anyone who configures buttons font globally like this, you will not be able to then customize font of some buttons differently. And second advice, if you continue work after someone else make sure that they give you some technical documentation or to explain to you what how and where they did.

EDIT

You can however set appearance for one view in viewWillAppear and then set it back to what you have in your CSAppDelegate.m in viewDidAppear of the view.

EDIT

Now this code is depreciated so you have to use this instead:

[[[UIButton appearance] titleLabel] setFont:UIFont];
Tomáš Chylý
  • 342
  • 1
  • 8
  • 21
1

The Problem seems to me is the Font name. The Font Name you are using in the code is not the exact name.

You Can Use the Following code to get List Of all Fonts name supported by your app

  // List all fonts on iPhone

for (NSString* family in [UIFont familyNames])
{
    NSLog(@"%@", family);

    for (NSString* name in [UIFont fontNamesForFamilyName: family])
    {
        NSLog(@"  %@", name);
    }
}

from the Console of xcode you can find out the exact name of the font that you added in your application

Anuj
  • 820
  • 4
  • 11
  • I have already written that Font is ok. And I have already created my answer to this question. Problem was with global UIButton appearance, you can read it in my answer. But still thanks for answering, this piece of code you wrote might come in handy in future. – Tomáš Chylý Sep 06 '13 at 12:54
-5

Please remove the AutoLayout checkmark if You used with Xib.It will works fine. I think its helps to you.

Friend
  • 7
  • 4