• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Aligning a subview element with text in an NSTextView (was: Finding vertical pos...)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Aligning a subview element with text in an NSTextView (was: Finding vertical pos...)


  • Subject: Re: Aligning a subview element with text in an NSTextView (was: Finding vertical pos...)
  • From: Douglas Davidson <email@hidden>
  • Date: Wed, 15 Dec 2004 15:44:28 -0800


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 not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: Aligning a subview element with text in an NSTextView (was: Finding vertical pos...)
      • From: Keith Blount <email@hidden>
References: 
 >Aligning a subview element with text in an NSTextView (was: Finding vertical pos...) (From: Keith Blount <email@hidden>)

  • Prev by Date: Controlling control sizes when programatically resizing a window
  • Next by Date: Re: Export to a Movie
  • Previous by thread: Aligning a subview element with text in an NSTextView (was: Finding vertical pos...)
  • Next by thread: Re: Aligning a subview element with text in an NSTextView (was: Finding vertical pos...)
  • Index(es):
    • Date
    • Thread