NSFont - 如何获得正确的字体?

发布于 2024-12-28 12:59:12 字数 790 浏览 2 评论 0原文

我想制作一个 NSFont 来描述 Arial,正常,高度 30pt。到目前为止我已经:

    NSNumber *weight = [NSNumber numberWithFloat:1.0];
    NSNumber *slant = [NSNumber numberWithFloat:1.0];
    NSDictionary *fontTraits = [NSDictionary dictionaryWithObjectsAndKeys: weight, NSFontWeightTrait, slant, NSFontSlantTrait, nil];
    NSDictionary *fontAttributes = [NSDictionary dictionaryWithObjectsAndKeys: @"Arial", NSFontFaceAttribute, 
                                                                               fontTraits, NSFontTraitsAttribute, nil];
    NSFontDescriptor *fontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes: fontAttributes];
    NSFont *largeFont = [NSFont fontWithDescriptor: fontDescriptor size: 30];

但是生成的 NSFont 大小不正确。我可以把任何我想要的尺寸放在那里,它们看起来都一样。

I would like to make a NSFont to describe Arial, normal, 30pt in height. So far I have:

    NSNumber *weight = [NSNumber numberWithFloat:1.0];
    NSNumber *slant = [NSNumber numberWithFloat:1.0];
    NSDictionary *fontTraits = [NSDictionary dictionaryWithObjectsAndKeys: weight, NSFontWeightTrait, slant, NSFontSlantTrait, nil];
    NSDictionary *fontAttributes = [NSDictionary dictionaryWithObjectsAndKeys: @"Arial", NSFontFaceAttribute, 
                                                                               fontTraits, NSFontTraitsAttribute, nil];
    NSFontDescriptor *fontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes: fontAttributes];
    NSFont *largeFont = [NSFont fontWithDescriptor: fontDescriptor size: 30];

but the resulting NSFont is not the right size. I can put any size I want in there and they all look the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和我恋爱吧 2025-01-04 12:59:12

您正在为 iOS 或 Mac OS X 编写内容?

这在我的 Mac 应用程序中运行良好:

NSFont* font = [NSFont fontWithName:@"Arial" size:30];

使用粗体和/或斜体更新:这对您来说足够了吗?

NSFont* font = [NSFont fontWithName:@"Arial Italic" size:30];
NSFont* font = [NSFont fontWithName:@"Arial Bold" size:30];
NSFont* font = [NSFont fontWithName:@"Arial Bold Italic" size:30];

UPDATE 2 可能会看看 NSFontManager

// convert font
NSFont* font = [NSFont fontWithName:@"Arial" size:30];
font = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSFontItalicTrait];

// create with traits and weight
NSFont* font = [[NSFontManager sharedFontManager]  fontWithFamily:@"Arial" traits:NSFontItalicTrait weight:2 size:30];

,但是使用这两个都无法创建粗细为 5 且倾斜度为 4 的字体。

我最近与一位设计师谈论了字体,他告诉我我认为通常像 Arial 这样的字体实际上有 4 种字体(即 Arial、Arial Italic、Arial Bold 和 Arial Bold Italic)。这 3 种其他字体样式(具有特征)不是由算法动态生成的。

are you writing for iOS or Mac OS X?

this works fine in my Mac App:

NSFont* font = [NSFont fontWithName:@"Arial" size:30];

UPDATE with bold and/or italic: is that enough for you ?

NSFont* font = [NSFont fontWithName:@"Arial Italic" size:30];
NSFont* font = [NSFont fontWithName:@"Arial Bold" size:30];
NSFont* font = [NSFont fontWithName:@"Arial Bold Italic" size:30];

UPDATE 2 may be take a look at NSFontManager

// convert font
NSFont* font = [NSFont fontWithName:@"Arial" size:30];
font = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSFontItalicTrait];

// create with traits and weight
NSFont* font = [[NSFontManager sharedFontManager]  fontWithFamily:@"Arial" traits:NSFontItalicTrait weight:2 size:30];

But with neither of those you are able to create a font with a weight of 5 and a slant of 4.

i recently talked to a designer about fonts and he told me that usually a font like Arial is actually 4 Fonts (i.e. Arial, Arial Italic, Arial Bold and Arial Bold Italic). those 3 other font styles (with the traits) are not generated on the fly by an algorithm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文