Ascertaining the top line in a view

  • Hi there

    I have a requirement for drawing line numbers next to a text view.
    Obviously the easiest way to do this in Cocoa is by means of a subclass
    of NSRuler. How all this works I can figure out, what is confusing me
    is how to ascertain the line number that is currently at the top of the
    NSTextView. In the case of unformatted text, i.e. one line == one
    paragraph, the number of paragraphs in the NSTextStorage is the number
    of lines, but how does one ascertain the relative positions of
    paragraphs with respect to the top of the NSTextView. I have spent
    hours reading NSTextStorage, NSLayoutManager, NSTextContainer, NSText,
    and NSTextView documentation without being able to figure this out. Has
    anyone any more in-depth knowledge on this subject that perhaps
    suggested a route ?

    Thanks

    Vincent
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • For the first character of each paragraph,
    charRange is NSMakeRange(first char offset, 1)
    noRange is NSMakeRange(NSNotFound, 0)
    container is [theTextView  container]
    layoutManager is [theTextView layoutManager]
    unsigned rectCount
    NSRect * rects = [layoutManager rectArrayForCharacterRange: charRange
      withinSelectedCharacterRange: noRange
      inTextContainer: container rectCount: &rectCount

    rectCount will always be 1. NSMinY(rects[0]) will be the top of the
    first line in the paragraph.

    For all the line-fragment rectangles,

    allChars is NSMakeRange(0, [textStorage length])
    allGlyphs is allChars converted to a glyph range by layoutManager
    currRange starts at NSMakeRange(0, 0)
    while NSMaxRange(currRange) < NSMaxRange(allGlyphs)
    currFragmentRect is [layoutManager lineFragmentRectForGlyphAtIndex:
    currRange.location
        effectiveRange: &currRange];
    process currFragmentRect. There may be more than one per line; look
    for the rect at the left margin
    currRange.location = NSMaxRange(currRange)
    currRange.length = 0;

    -- F

    On 7 Nov 2003, at 1:11 AM, Vincent Coetzee wrote:

    > Hi there
    >
    > I have a requirement for drawing line numbers next to a text view.
    > Obviously the easiest way to do this in Cocoa is by means of a
    > subclass of NSRuler. How all this works I can figure out, what is
    > confusing me is how to ascertain the line number that is currently at
    > the top of the NSTextView. In the case of unformatted text, i.e. one
    > line == one paragraph, the number of paragraphs in the NSTextStorage
    > is the number of lines, but how does one ascertain the relative
    > positions of paragraphs with respect to the top of the NSTextView. I
    > have spent hours reading NSTextStorage, NSLayoutManager,
    > NSTextContainer, NSText, and NSTextView documentation without being
    > able to figure this out. Has anyone any more in-depth knowledge on
    > this subject that perhaps suggested a route ?
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Hi there

    Thanks for your help guys, but I found a reference to the exact
    solution I was looking for on MamaSan, thanks to Koen van der Drift and
    Jim Derry. Thanks for publishing the source guys.

    Vince

    On Nov 07, 2003, at 19:05, Fritz Anderson wrote:

    > For the first character of each paragraph,
    > charRange is NSMakeRange(first char offset, 1)
    > noRange is NSMakeRange(NSNotFound, 0)
    > container is [theTextView  container]
    > layoutManager is [theTextView layoutManager]
    > unsigned rectCount
    > NSRect * rects = [layoutManager rectArrayForCharacterRange: charRange
    > withinSelectedCharacterRange: noRange
    > inTextContainer: container rectCount: &rectCount
    >
    > rectCount will always be 1. NSMinY(rects[0]) will be the top of the
    > first line in the paragraph.
    >
    > For all the line-fragment rectangles,
    >
    > allChars is NSMakeRange(0, [textStorage length])
    > allGlyphs is allChars converted to a glyph range by layoutManager
    > currRange starts at NSMakeRange(0, 0)
    > while NSMaxRange(currRange) < NSMaxRange(allGlyphs)
    > currFragmentRect is [layoutManager lineFragmentRectForGlyphAtIndex:
    > currRange.location
    > effectiveRange: &currRange];
    > process currFragmentRect. There may be more than one per line; look
    > for the rect at the left margin
    > currRange.location = NSMaxRange(currRange)
    > currRange.length = 0;
    >
    > -- F
    >
    > On 7 Nov 2003, at 1:11 AM, Vincent Coetzee wrote:
    >
    >> Hi there
    >>
    >> I have a requirement for drawing line numbers next to a text view.
    >> Obviously the easiest way to do this in Cocoa is by means of a
    >> subclass of NSRuler. How all this works I can figure out, what is
    >> confusing me is how to ascertain the line number that is currently at
    >> the top of the NSTextView. In the case of unformatted text, i.e. one
    >> line == one paragraph, the number of paragraphs in the NSTextStorage
    >> is the number of lines, but how does one ascertain the relative
    >> positions of paragraphs with respect to the top of the NSTextView. I
    >> have spent hours reading NSTextStorage, NSLayoutManager,
    >> NSTextContainer, NSText, and NSTextView documentation without being
    >> able to figure this out. Has anyone any more in-depth knowledge on
    >> this subject that perhaps suggested a route ?
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month november 2003 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today
MindNode
MindNode offered a free license !