iOS Quick Tip: Find the Size of a NSString

Stephen Zaharuk / Tuesday, June 24, 2014

When writing an app, you sometimes comes across situations where you need to adjust the position and size of a label based on the size of the text. And that size could be different based on a particular font you're using. So, today i'm going to show you how you can quickly grab the size of a string with a specified font. 

Prior to iOS 7 you could use:

CGSize size = [@“Infragistics" sizeWithFont:[UIFont fontWithName:@"Avenir-Book" size:20]];

However, in iOS 7 that method was deprecated. Instead you now must use: 

CGSize size = [@“Infragistics” sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:@“Avenir-Book” size:20]}];

Enjoy!

By Stephen Zaharuk (SteveZ)