【问题标题】:Obtaining the maximum height of a font获取字体的最大高度
【发布时间】:2010-09-18 19:45:01
【问题描述】:

所以我有一个 NSFont,我想获得任何字符的最大尺寸,即。间距和字母高度。 [font maximumAdvancement] 似乎返回 {pitch, 0} 的 NSSize,所以这没有帮助。边界矩形似乎也不起作用,jwz's similar question 提出的创建贝塞尔路径、附加字形和获取边界矩形的建议也让我返回 {0, 0}。这里给出了什么?

更新:我用来获取贝塞尔大小的代码是这样的:

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

【问题讨论】:

    标签: objective-c cocoa fonts rendering


    【解决方案1】:

    空格字符的字形没有任何子路径,所以它的边界当然是NSZeroSize。请改用-[NSFont boundingRectForFont]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 2012-11-04
      • 2013-06-14
      • 2018-03-22
      • 2015-12-14
      • 2012-12-25
      • 2012-07-09
      相关资源
      最近更新 更多