Get NSTextField contents to scale

前端 未结 3 1830
再見小時候
再見小時候 2020-12-01 05:43

How can I have the text scale to fit the bounds I gave it?

3条回答
  •  臣服心动
    2020-12-01 06:19

    I've done something like this in the past.

    -(void)calcFontSizeToFitRect:(NSRect)r {
        float targetWidth = r.size.width - xMargin;
        float targetHeight = r.size.height - yMargin;
    
        // the strategy is to start with a small font size and go larger until I'm larger than one of the target sizes
        int i;
        for (i=minFontSize; i targetWidth || strSize.height > targetHeight) break;
        }
        [self setCurrentFontSize:(i-1)];
    }
    

    The string variable is the text you want sized. The xMargin and yMargin variables are for spacing that you want. The minFontSize and maxFontSize variables give limits to how small or large you want to go.

提交回复
热议问题