So,
in sizeWithFont:minFontSize:actualFontSize, we pass a pointer for actualFontSize which can then be used to shrink the font within a label. As in :
[s sizeWithFont:self.font
minFontSize:minimumScaleFactor
actualFontSize:&actualFontSize
forWidth:width
lineBreakMode:lineBreakMode];
So now, we can do self.frame.font.pointSize = actualSize to modify the font size. My question is : Now that sizeWithFont is deprecated, how do I get this pointer back?
Basically, the recommended method to replace it is boundingRectWithSize :
[s boundingRectWithSize:CGSize(width,CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin)
attributes://An NSDictionary with font
context:nil];
But as you can see, it only returns the size. I thought of doing something like :
changeInFontSize = (newSize.height - oldSize.height) + (newSize.width - oldSize.width)
-------------------------------------------------------------------
2
But this doesn't really give me the answer I want. Could someone help me about how to do this? To be precise - I need the difference in the font size to change the current font size!
The problem is that
sizeWithFontmethod reduces the fontsize as needed. So if the function used a smaller font size, how do you know which size it used? That's theactualFontSizefor!! From the documentation:The new recommended method
boundingRectWithSizedoes not reduce the fontsize. So if the function does NOT reduce the fonsize, meaning, it will use the size you specify inattributes, there's no need to return the actual size used (because it's exactly the one you specified, hence, you already know it).EDIT:
If you want to calculate the font size needed to fit in some predefined bounding box, you would need to test with a few point sizes and calculate the one that fits best. Check this link for some examples (the last answer on the link looks interesting to optimize efficiency)