Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Thursday, September 26, 2013

Programmatically Setting Font-Size In iOS

So, your heart's desire is to adjust the font-size of a label or button programmatically in iOS, eh?

Lets say your design calls for a fixed-size box into which you should place some content. Naturally, you reach for a UILabel, but then you realize that you need to accommodate varying amounts of text inside of it. So, you start reaching for a UIWebView, but then you're like, "Hmmm ... setting the font-size of this thing programmatically shouldn't be too hard... lets ask the Googles."

Well, friend, the Googles have lead you to the right corner of the web. For here is the answer you seek (probably):

Now, first things first, I want to do things all right and proper, so I define my font-sizes as constants in my header file (you should too!):

#define font_size_for_num_digits_1 100.0f
#define font_size_for_num_digits_2 80.0f
#define font_size_for_num_digits_3 60.0f
#define font_size_for_num_digits_4 40.0f
#define font_size_for_num_digits_5_and_up 20.0f

Now, I want to create a nice method for determining which font-size I should use in various conditions. In my case, my field contains numbers, and while it would be just as easy to make this method handle it as strings, I'll leave the exercise to the reader. But, if you're really hurting, reach out to me in the comments and I'll help you. Anyways:

  int val = 0.0f;
  // "Egads, if-else statements offend my OOP Spidey-Senses"
  // Well, mine too... but iOS doesn't like relative comparisons in their 
  // switch statements, so what's a guy to do?
  if (num < 10){
    val = font_size_for_digits_1;
  } else if (num >= 10 && num < 100){
    val = font_size_for_digits_2;
  } else if (num >= 100 && num < 1000){
    val = font_size_for_digits_3;
  } else if (num >= 1000 && num < 10000){
    val = font_size_for_digits_4;
  } else if (num >= 10000 && num < 100000){
    val = font_size_for_digits_5;
  return val;

So, somewhere later in my code, say in a setter for my label (or buttons) text value, I do thusly:

float size = [BigNumberCell getFontSizeForNumber:num];
[[self myLabel] setFont:[UIFont systemFontOfSize:font_size]];

There's an important note to make here before you haul off uninformed like so many people before you... this right here: [UIFont systemFontOfSize:font_size] has the awesome effect of PRESERVING YOUR CURRENT FONT... so that you don't need to monkey with fontWithName.

Did you hear that last part? {sigh} You're probably gone already and you'll either be confused or you'll take it for granted.

If you're still here, good on ya, see you next time on the Googles!

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates:

Popular Posts

Designed By Seo Blogger Templates