Dave123
Dave123

Reputation: 316

How can I change the font size of a uilabel without changing the font itself?

I'm having an issue where I allow the user to select a font for a label in my project, but then when the user sets the size of that label (using a different button), the label resets to a default font. I'd like to be able to retain the font that the user had applied while still allowing the user to change the font size. Any help is appreciated, thanks!

Here's my code..

-(IBAction)setFont{

    [userText setFont:[UIFont fontWithName:@"Arial-BoldMT" size:50.0]];

//I had to add a size when setting the font or else I got an error


}



-(IBAction)setFontSize{

  [userText setFont:[UIFont systemFontOfSize:24]];

}

Upvotes: 8

Views: 12897

Answers (2)

rmaddy
rmaddy

Reputation: 318824

Just use the fontWithSize: method on the label's current font:

- (IBAction)setFontSize {
    // Keep the same font but change its size to 24 points.
    UIFont *font = userText.font;
    userText.font = [font fontWithSize:24];
}

Upvotes: 31

Valent Richie
Valent Richie

Reputation: 5226

The function [UIFont systemFontOfSize:] will always return default system font. You can just make use of the same function that you call in setFont which is [UIFont fontWithName:size:].

Upvotes: 1

Related Questions