如何在文本单元格中设置字体大小,以便字符串填充单元格的rect? [英] How do I set the font size in a text cell so that the string fills the cell's rect?

查看:170
本文介绍了如何在文本单元格中设置字体大小,以便字符串填充单元格的rect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图,包含两个 NSTextFieldCell 。绘制这些单元格的大小是从视图的大小导出的,我希望每个单元格中的文本是最大的,适合于单元格的导出大小。这里是我有,它不设置字体大小:

I have a view that contains two NSTextFieldCells. The size at which these cells are drawn is derived from the size of the view, and I want the text in each cell to be the largest that will fit in the derived size of the cell. Here's what I have, which doesn't set the font size:

- (void)drawRect:(NSRect)dirtyRect {
    /*
     * Observant readers will notice that I update the whole view here. If
     * there is a perceived performance problem, then I'll switch to just
     * updating the dirty rect.
     */
    NSRect boundsRect = self.bounds;
    const CGFloat monthHeight = 0.25 * boundsRect.size.height;
    NSRect monthRect = NSMakeRect(boundsRect.origin.x,
                                  boundsRect.origin.y + boundsRect.size.height
                                  - monthHeight,
                                  boundsRect.size.width,
                                  monthHeight);
    [monthCell drawWithFrame: monthRect inView: self];

    NSRect dayRect = NSMakeRect(boundsRect.origin.x,
                                boundsRect.origin.y,
                                boundsRect.size.width,
                                boundsRect.size.height - monthHeight);
    [dayCell drawWithFrame: dayRect inView: self];

    [[NSColor blackColor] set];
    [NSBezierPath strokeRect: boundsRect];
}

所以我知道我可以问一个字符串属性,我知道我可以要求控件更改其大小以适应其内容。这些都不适用:我想要的内容(在这种情况下,单元格的 stringValue )的大小以适应已知的rect维度,具有实现那个未知的属性。如何找到所需的尺寸?假设我知道我将使用什么字体(因为我这样做)。

So I know that I can ask a string what size it would take for given attributes, and I know that I can ask a control to change its size to fit its content. Neither of those seems applicable: I want the content (in this case, the cell's stringValue) to size to fit the known rect dimensions, with the attributes needed to achieve that being unknown. How can I find the needed size? Assume that I know what font I'll be using (because I do).

更新注意:我不想< > truncate 字符串,我想要成长收缩,使整个事情适合,尽可能最大的文字大小, / p>

update Note: I don't want to truncate the string, I want to grow or shrink it so that the whole thing fits, with the largest text size possible, into the provided rect.

推荐答案

我使用一些类似的代码,但它处理不同的字体,大小高达10,000,并考虑到可用的高度和宽度

I use some similar code but it handles different fonts, sizes up to 10,000 and takes into account the available height as well as width of the area the text is being displayed in.

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

这篇关于如何在文本单元格中设置字体大小,以便字符串填充单元格的rect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆