【问题标题】:How do I find the pixel position of a character in an NSTextView?如何在 NSTextView 中找到字符的像素位置?
【发布时间】:2009-05-15 14:36:29
【问题描述】:

我目前正在使用以下代码来获取字形的逻辑 NSView 位置,该位置运行良好,但它似乎有点慢,并且位置并不精确地位于字符字距调整点和基线上。很有可能我需要做的就是一个一个地迭代字形本身并自己构建矩形。

现在这段代码(函数),就是想看看能不能更高效、更正确地完成。

// lastRange is any valid, bounds checked character range in an NSTextView


NSLayoutManager *layoutManager = [self layoutManager];
NSRect paragraphRect = [layoutManager boundingRectForGlyphRange:lastRange inTextContainer:[self textContainer]];

// Draw a gradient around the range.
NSGradient * gradient = [self indicatorGradient];
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetBlendMode(myContext , kCGBlendModeMultiply);
[gradient drawInRect:paragraphRect angle:90];

【问题讨论】:

    标签: objective-c cocoa nstextview


    【解决方案1】:

    这是实现它的方法。但是,如果 lastRange 是您所描述的 characterRange,则必须通过

    lastRange = [layoutManager glyphRangeForCharacterRange:lastRange actualRange:NULL];
    

    首先得到正确的结果。 您还需要确保考虑文本布局的文本容器的来源,然后执行

    NSPoint containerOrigin = [[container textView] textContainerOrigin];
    paragraphRect = NSOffsetRect(paragraphRect,containerOrigin.x,containerOrigin.y);
    

    绘制之前。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      相关资源
      最近更新 更多