代码之家  ›  专栏  ›  技术社区  ›  Josh Matthews

获取字体的最大高度

  •  0
  • Josh Matthews  · 技术社区  · 16 年前

    所以我有一个NSFont,我想得到任何字符的最大尺寸,即音高和字母高度。[font maximumAdvancement]似乎返回的NSSize为{pitch,0},因此这没有帮助。Bounding rect似乎也不起作用,来自 jwz's similar question

    使现代化 :我用于获取贝塞尔尺寸的代码如下:

    NSBezierPath *bezier = [NSBezierPath bezierPath];
    NSGlyph g;
    {
        NSTextStorage *ts = [[NSTextStorage alloc] initWithString:@" "];
        [ts setFont:font];
        NSLayoutManager *lm = [[NSLayoutManager alloc] init];
        NSTextContainer *tc = [[NSTextContainer alloc] init];
        [lm addTextContainer:tc];
        [tc release]; // lm retains tc
        [ts addLayoutManager:lm];
        [lm release]; // ts retains lm
        g = [lm glyphAtIndex:0];
        [ts release];
    }
    NSPoint pt = {0.0f};
    [bezier moveToPoint:pt];
    [bezier appendBezierPathWithGlyph:g inFont:font];
    NSRect bounds = [bezier bounds];
    
    1 回复  |  直到 16 年前
        1
  •  6
  •   Peter Hosey    16 年前

    空格字符的标志符号没有任何子路径,因此其边界当然有大小 NSZeroSize . 尝试 -[NSFont boundingRectForFont] 相反