开发者

How can I calculate (without search) a font size to fit a rect?

开发者 https://www.devze.com 2023-04-01 02:19 出处:网络
I want my text to fit within a specific rect, so I need something to determine a font size.Questions have already tackled this to an extent, but they do a search, which seems horribly inefficient, esp

I want my text to fit within a specific rect, so I need something to determine a font size. Questions have already tackled this to an extent, but they do a search, which seems horribly inefficient, especially if you want to be able to calculate during a live dragging resize. The following example could be improved to binary search and by constraining to the height, but it is still a search. Instead of searching, how can I calculate a font size to fit a rect?

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


Pick any font size and measure the text at that size. Divide each of its dimensions (width and height) by the same dimension of your target rectangle, then divide the font size by the larger factor.

Note that the text will measure on one line, since there is no maximum width for it to wrap to. For a long line/string, this may result in an unusefully-small font size. For a text field, you should simply enforce a minimum size (such as the small system font size), and set the field's truncation behavior. If you intend to wrap the text, you'll need to measure it with something that takes a bounding rectangle or size.

Code by asker roughly 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.];//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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消