Search code examples
iphoneobjective-ciosxcodexcode4

How to calculate size of a string using custom font


I have a Label that displays some text like "I am good". How can I calculate the size of text in Label that has custom font. I am calculating size like

CGSize s = [mSubLabel.text sizeWithFont:[UIFont fontWithName:@"ArnhemPro-Blond.otf" size:10.0] constrainedToSize:Max_Size lineBreakMode:UILineBreakModeTailTruncation];

NSLog(@"expected size %@",NSStringFromCGSize(s));

But the size is (0,0)

How can i calculate the size? using GSize subLabelSize=[mSubLabel.text sizeWithFont:[UIFont systemFontOfSize:1] constrainedToSize:Max_Size lineBreakMode:UILineBreakModeTailTruncation];

gives me size but that is not correct.help please...


Solution

  • Your font is most likely nil, the name parameter for fontNamed: should be the name of the font which is usually different from its filename (and has no extension). You can use the Font Book app to find out the actual name of a font.