Thanks Sam. What I am wanting to do is to draw an underlying filled rect for what ever line # specified.
What I have discovered… even with mono-spaced fonts, the linespacing can be vastly different … for example 40pt Menlo is 46pts, while 40pt Courier is 48pts…
and Courier takes up less vertical space withing that 48pt.
And to make matters worse… to line text in a Canvas (NSView) to text in an NSTextView (both with the same font and size)…
The Canvas needs to be vertically offset by 9.6pts for Menlo, but only 1pt for Courier
These values were derived from tons of experiments, and attempting to find some equation I could apply using metrics from the fonts … to no avail
Well this doesn’t give me the bounding rectangle… but it does “select” the desired line…
func selectLine(textView: NSTextView,lineNumberToSelect : Int) -> Int {
let layoutManager = textView.layoutManager
var numberOfLines = 0
var index = 0
var lineRange = NSRange()
let numberOfGlyphs = layoutManager!.numberOfGlyphs
while index < numberOfGlyphs {
numberOfLines += 1
layoutManager!.lineFragmentRect(forGlyphAt: index, effectiveRange: &lineRange)
if numberOfLines == lineNumberToSelect {
textView.setSelectedRange(lineRange)
break
}
index = NSMaxRange(lineRange);
}
}
and I should be able to derive the bounds from the Scroll bar positions and the linespacing both of which I know. There is no word-wrap in this editor, so that makes it a simple multiple and add