【问题标题】:How can I calculate (without search) a font size to fit a rect?如何计算(无需搜索)字体大小以适合矩形?
【发布时间】:2011-09-01 14:49:50
【问题描述】:

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

#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;
}

【问题讨论】:

    标签: cocoa nstextfield


    【解决方案1】:

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

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

    asker 的代码大致基于这个想法:

    -(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];
    }
    

    【讨论】:

    • 我喜欢它并将发布我想出的东西。
    • Jerry Krinock 的 NS(Attributed)String+Geometrics 类别对于测量文本非常宝贵,您应该检查一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多