获取 NSTextField 内容以进行缩放 [英] Get NSTextField contents to scale

查看:29
本文介绍了获取 NSTextField 内容以进行缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让文本比例适合我给它的边界?

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

推荐答案

我过去做过类似的事情.

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<maxFontSize; i++) {
        NSDictionary* attrs = [[NSDictionary alloc] initWithObjectsAndKeys:[NSFont fontWithName:currentFontName size:i], NSFontAttributeName, nil];
        NSSize strSize = [stringValue sizeWithAttributes:attrs];
        [attrs release];
        if (strSize.width > targetWidth || strSize.height > targetHeight) break;
    }
    [self setCurrentFontSize:(i-1)];
}

stringValue 变量是您想要调整大小的文本.xMarginyMargin 变量用于您想要的间距.minFontSizemaxFontSize 变量限制了您想要的大小.

The stringValue 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.

这篇关于获取 NSTextField 内容以进行缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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