Skip navigation.
 
mlRe: Aligning a subview element with text in an NSTextView (was: Finding vertical pos...)
FROM : Keith Blount
DATE : Thu Dec 16 20:52:55 2004

Many thanks for your reply, much appreciated. This
looks like exactly the sort of start I need, I'm
looking forward to trying to implement it for my needs
tomorrow.

Thanks again for your help,
Keith

--- Douglas Davidson <<email_removed>> wrote:

>
> On Dec 15, 2004, at 10:19 AM, Keith Blount wrote:
>
>
> > Hi, sorry to reply to my own post, but I have
> spent
> > another day trying to get this right with no luck.
> >
> > Does anybody know how to constantly get the
> vertical
> > position of a character in an NSTextView, even if
> the
> > view's size is changed or after the text is
> edited? I
> > need to align little notes that are added by the
> user
> > with the character nearest where the note was
> added...
> > So I have to keep track of a particular character
> (or
> > rather, a number of characters) and tell the notes
> > views whenever the vertical positions of these
> > characters changes for any reason.
>
> Here is a snippet of code from a previous WWDC
> example.  This is a
> method on a custom NSTextView subclass intended to
> draw bubbles around
> each individual paragraph in the text.  It's not
> directly on point for
> what you are after, but if you understand how it
> works you should
> understand how to use the layout manager to
> determine the location of
> any piece of text.
>
> Douglas Davidson
>
> - (void)drawViewBackgroundInRect:(NSRect)rect {
>      NSLayoutManager *layoutManager = [self
> layoutManager];
>      NSPoint containerOrigin = [self
> textContainerOrigin];
>      NSRange glyphRange, charRange,
> paragraphCharRange,
> paragraphGlyphRange, lineGlyphRange;
>      NSRect paragraphRect, lineUsedRect;
>
>      // Draw the background first, before the
> bubbles.
>      [super drawViewBackgroundInRect:rect];
>
>      // Convert from view to container coordinates,
> then to the
> corresponding glyph and character ranges.
>      rect.origin.x -= containerOrigin.x;
>      rect.origin.y -= containerOrigin.y;
>      glyphRange = [layoutManager
> glyphRangeForBoundingRect:rect
> inTextContainer:[self textContainer]];
>      charRange = [layoutManager
> characterRangeForGlyphRange:glyphRange
> actualGlyphRange:NULL];
>
>      // Iterate through the character range,
> paragraph by paragraph.
>      for (paragraphCharRange =
> NSMakeRange(charRange.location, 0);
> NSMaxRange(paragraphCharRange) <
> NSMaxRange(charRange);
> paragraphCharRange =
> NSMakeRange(NSMaxRange(paragraphCharRange), 0)) {
>          // For each paragraph, find the
> corresponding character and
> glyph ranges.
>          paragraphCharRange = [[[self textStorage]
> string]
> paragraphRangeForRange:paragraphCharRange];
>          paragraphGlyphRange = [layoutManager
> glyphRangeForCharacterRange:paragraphCharRange
> actualCharacterRange:NULL];
>          paragraphRect = NSZeroRect;
>
>          // Iterate through the paragraph glyph
> range, line by line.
>          for (lineGlyphRange =
> NSMakeRange(paragraphGlyphRange.location,
> 0); NSMaxRange(lineGlyphRange) <
> NSMaxRange(paragraphGlyphRange);
> lineGlyphRange =
> NSMakeRange(NSMaxRange(lineGlyphRange), 0)) {
>              // For each line, find the used rect
> and glyph range, and
> add the used rect to the paragraph rect.
>              lineUsedRect = [layoutManager
>

lineFragmentUsedRectForGlyphAtIndex:lineGlyphRange.location
>
> effectiveRange:&lineGlyphRange];
>              paragraphRect =
> NSUnionRect(paragraphRect, lineUsedRect);
>          }
>
>          // Convert back from container to view
> coordinates, then draw
> the bubble.
>          paragraphRect.origin.x +=
> containerOrigin.x;
>          paragraphRect.origin.y +=
> containerOrigin.y;
>          [self
> drawBubbleAroundTextInRect:paragraphRect];
>    }
> }
>
>
>



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Related mailsAuthorDate
mlAligning a subview element with text in an NSTextView (was: Finding vertical pos...) Keith Blount Dec 15, 19:19
mlRe: Aligning a subview element with text in an NSTextView (was: Finding vertical pos...) Douglas Davidson Dec 16, 00:44
mlRe: Aligning a subview element with text in an NSTextView (was: Finding vertical pos...) Keith Blount Dec 16, 20:52