Re: Adjusting Font Size to fit View Rectangle


Alex Zavatone
 

You also might want to check the height of a descender appears below the bounding NSTextField.

On Sep 20, 2018, at 5:21 AM, Dave <dave@looktowindward.com> wrote:

Hi,

Couldn’t find it so wrote my own:

(NSInteger) adjustFontSize:(NSInteger) theFontSize toFitWidth:(NSInteger) theWidth
{
NSInteger myFontSize;
NSFont* myFont;
NSSize myContentSize;

myFontSize = theFontSize;
while (YES)
{
myFont = [NSFont fontWithName:self.font.fontName size:myFontSize];
[self setFont:myFont];

myContentSize = [self.attributedStringValue size];
if (myContentSize.width < theWidth)
break;

myFontSize--;
if (myFontSize <= 0)
break;
}

return myFontSize;
}

This is on an NSTextField subclass and works a treat!

All the Best
Dave

On 20 Sep 2018, at 11:51, Dave <dave@looktowindward.com> wrote:

Hi,

Those two methods alter the frame size, I want to keep the same Frame Size but reduce the Font Size of the Content until it fit into the Displayable Area. I’m 99% sure there is a method to do it but maybe its iOS only?

All the Best
Dave

On 20 Sep 2018, at 02:02, Alex Zavatone via Groups.Io <zav=mac.com@groups.io> wrote:

There are two that I am aware of. sizeToFit and sizeThatFits.
On Sep 19, 2018, at 1:29 PM, Dave <dave@looktowindward.com> wrote:

Hi All,

Is there a Cocoa method I can use that will return the best fitting Font Size for a given String and Font?

I seem to remember using a method that does it but can’t seem to find it any more…..

All the Best
Dave







Join cocoa@apple-dev.groups.io to automatically receive all group messages.