jdl
jdl

Reputation: 6333

UILabel: how do you set the font size?

How do you set the font size from a UILabel?

My code:

UILabel *myView = [[UILabel alloc] initWithFrame:RectFrame];
[myView setBackgroundColor:[UIColor blueColor]];
[myView setText:[NSString stringWithFormat:@" A"]];
[myView setFont:[12] ]; <--- Error
[self.view addSubview:myView];

Upvotes: 42

Views: 61771

Answers (7)

Dilip Jangid
Dilip Jangid

Reputation: 774

Swift 4 and above.

You may also try it.

label.font = UIFont.systemFont(ofSize: 10) 

Upvotes: 3

kamalesh kumar yadav
kamalesh kumar yadav

Reputation: 966

In swift

yourLabel.font = UIFont(name:"name of font-family you want", size: 9)

Upvotes: 2

karan
karan

Reputation: 3369

If your UILabel's font is defined in IB storyboard and you just want to increase size then this will work, worked for me ;)

[_label setFont: [_label.font fontWithSize: sizeYouWant]];

Or

[self.label setFont: [self.label.font fontWithSize: sizeYouWant]];

Another method as others says also worked:

[_label setFont:[UIFont systemFontOfSize:13.0]];

Upvotes: 8

gnasher729
gnasher729

Reputation: 52622

Remember that the user can globally set how large they want their text to be, so you shouldn't use a fixed font size like "12". Use the following fonts:

[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]

[UIFont preferredFontForTextStyle:UIFontTextStyleBody]
[UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]
[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]
[UIFont preferredFontForTextStyle:UIFontTextStyleCaption1]
[UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]

I think the default for UILabel is Caption1, the others will be bigger or smaller. And they will adapt if the user wants big text or small text on his or her device. (Font size 12 might actually bigger than the normal label size if the user picked a very small size).

Upvotes: 4

virantporwal
virantporwal

Reputation: 987

UILabel *myView = [[UILabel alloc] initWithFrame:RectFrame];

[myView setBackgroundColor:[UIColor blueColor]];

[myView setText:[NSString stringWithFormat:@" A"]];

[myView setFont:[UIFont systemFontOfSize:12]];

[self.view addSubview:myView];

Upvotes: 3

knuku
knuku

Reputation: 6102

Check UIFont class. After that you'll probably get why you should use it like that:

[myView setFont:[UIFont systemFontOfSize:12]];

Upvotes: 6

CristiC
CristiC

Reputation: 22708

[myView setFont:[UIFont systemFontOfSize:12]];

or

[myView setFont:[UIFont boldSystemFontOfSize:12]];

or for font family

[myView setFont:[UIFont fontWithName:@"Helvetica" size:12]];

Upvotes: 120

Related Questions