Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change custom UIFont size on the UIButton

I have a button that I use custom UIFont to display text on. The font loads correctly and is correctly applied to the button title. My problem is I can't seem to be able to change font size:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(loginButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Login" forState:UIControlStateNormal];
button.frame = CGRectMake(63.0, 200.0, 194.0, 60.0);
button.titleLabel.font = [UIFont fontWithName:@"My-Font" size:8.0f];
[self.view addSubview:button];

No matter what I put in for font size, I get some default size. On the other hand, if I do something like this:

button.titleLabel.font = [UIFont systemFontOfSize:32.0f];

I get size 32 font, however, of course, I don't get my custom font. So, how do I set a size for custom font?

(I use Xcode 4.6.3)

like image 786
mike.tihonchik Avatar asked Jul 05 '13 17:07

mike.tihonchik


People also ask

How do you change the font on UILabel?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.


2 Answers

Seems like your UIFont object isn't getting created correctly and is returning nil.

This is the best tutorial I have found on putting in custom fonts. Good luck ;)

http://refactr.com/blog/2012/09/ios-tips-custom-fonts/ (Archived Version)

like image 89
Matt Mc Avatar answered Sep 23 '22 20:09

Matt Mc


To get the font name you have to pass in fontWithName:size:, open your font with Font Book app, then look up for the PostScript name property. Make shure you have added it to Info.plist file too.

like image 21
leoformaggio Avatar answered Sep 21 '22 20:09

leoformaggio