简体   繁体   English

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

[英]NSFont - How to get the right font?

I would like to make a NSFont to describe Arial, normal, 30pt in height. 我想让NSFont描述Arial,正常,高度为30pt。 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. 但由此产生的NSFont尺寸不合适。 I can put any size I want in there and they all look the same. 我可以放任何我想要的尺寸,它们看起来都一样。

are you writing for iOS or Mac OS X? 你在为iOS或Mac OS X写作吗?

this works fine in my Mac App: 这在我的Mac App中运行正常:

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

UPDATE with bold and/or italic: is that enough for you ? UPDATE以大胆和/或斜体:是够吗?

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 更新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];

But with neither of those you are able to create a font with a weight of 5 and a slant of 4. 但是,这些都不能创建权重为5且倾斜度为4的字体。

i recently talked to a designer about fonts and he told me that usually a font like Arial is actually 4 Fonts (ie Arial, Arial Italic, Arial Bold and Arial Bold Italic). 我最近和一位设计师谈过字体,他告诉我通常像Arial这样的字体实际上是4个字体(即Arial,Arial Italic,Arial Bold和Arial Bold Italic)。 those 3 other font styles (with the traits) are not generated on the fly by an algorithm. 其他3种其他字体样式(具有特征)不是由算法动态生成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM