如何计算(不搜索)字体大小以适合矩形?

Pet*_*ese 4 cocoa nstextfield

我希望我的文本适合特定的矩形,所以我需要一些东西来确定字体大小. 问题已经在一定程度上解决了这个问题,但是它们会进行搜索,这看起来非常低效,特别是如果您希望能够在实时拖动调整大小期间进行计算.以下示例可以改进为二进制搜索并通过约束到高度,但它仍然是搜索.而不是搜索,我如何计算适合矩形的字体大小?

#define kMaxFontSize    10000

- (CGFloat)fontSizeForAreaSize:(NSSize)areaSize withString:(NSString *)stringToSize usingFont:(NSString *)fontName;
{
    NSFont * displayFont = nil;
    NSSize stringSize = NSZeroSize;
    NSMutableDictionary * fontAttributes = [[NSMutableDictionary alloc] init];

    if (areaSize.width == 0.0 && areaSize.height == 0.0)
        return 0.0;

    NSUInteger fontLoop = 0;
    for (fontLoop = 1; fontLoop <= kMaxFontSize; fontLoop++) {
        displayFont = [[NSFontManager sharedFontManager] convertWeight:YES ofFont:[NSFont fontWithName:fontName size:fontLoop]];
        [fontAttributes setObject:displayFont forKey:NSFontAttributeName];
        stringSize = [stringToSize sizeWithAttributes:fontAttributes];

        if (stringSize.width > areaSize.width)
            break;
        if (stringSize.height > areaSize.height)
            break;
    }

    [fontAttributes release], fontAttributes = nil;

    return (CGFloat)fontLoop - 1.0;
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*sey 12

选择任何字体大小并测量该大小的文本.将每个尺寸(宽度和高度)除以目标矩形的相同尺寸,然后将字体大小除以较大的系数.

请注意,文本将在一行上测量,因为它没有最大宽度可以换行.对于长行/字符串,这可能会导致字体大小无用.对于文本字段,您应该只强制执行最小大小(例如小系统字体大小),并设置字段的截断行为.如果您打算包装文本,则需要使用带有边界矩形或大小的内容来测量它.

提问者的代码大致基于这个想法:

-(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.];//use standard size to prevent error accrual
    CGSize sampleSize = [string sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:sampleFont, NSFontAttributeName, nil]];
    float scale = [self scaleToAspectFit:sampleSize into:size padding:10];
    return [NSFont fontWithDescriptor:font.fontDescriptor size:scale * sampleFont.pointSize];
}

-(void)windowDidResize:(NSNotification*)notification
{
    text.font = [self fontSizedForAreaSize:text.frame.size withString:text.stringValue usingFont:text.font];
}
Run Code Online (Sandbox Code Playgroud)