Choose any font size and measure text of that size. Divide all its sizes (width and height) by the same size of your target rectangle, then divide the font size by a larger factor.
Please note that the text will be measured in one line, since there is no maximum width for it. For a long line / line, this can lead to a small font size. For a text field, you just need to enter the minimum size (for example, the small font size of the system) and set the behavior of the field truncation. If you intend to wrap the text, you will need to measure it using what takes a bounding box or size.
Survey code based on this idea:
-(float)scaleToAspectFit:(CGSize)source into:(CGSize)into padding:(float)padding { return MIN((into.width-padding) / source.width, (into.height-padding) / source.height); } -(NSFont*)fontSizedForAreaSize:(NSSize)size withString:(NSString*)string usingFont:(NSFont*)font; { NSFont* sampleFont = [NSFont fontWithDescriptor:font.fontDescriptor size:12.];
source share