Getting the rect of a character in an NSTextView
Getting the rect of a character in an NSTextView
- Subject: Getting the rect of a character in an NSTextView
- From: Keith Blount <email@hidden>
- Date: Sun, 23 Jan 2005 14:44:10 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
Hello,
I have a margin view associated with a text view which
contains a number of note subviews. Each of these
"notes" is associated with the index of a character in
the text view, so that they can be moved around
whenever the text is updated in the text view.
Everything has been working fine, but I have now
started testing with very large files, and I am
getting consistent crashes during live resize of my
view. The crash seems to be caused by the method I use
to calculate the point in the textview of the charact
a note card is attached to. This method is supposed to
return the rect of the part of any line starting with
passed-in character index. If anybody can see why this
method might be crashing my prog, I would be very
grateful:
// Returns the rect of the character at the given
index
// (actually it returns the rect of the line from the
character onwards, but that's all we need it to do)
- (NSRect)rectForCharacterIndex:(unsigned
int)charIndex
{
NSLayoutManager *layoutManager = [textView
layoutManager];
NSPoint containerOrigin = [textView
textContainerOrigin];
NSRect r;
int glyphIndex = [layoutManager
glyphRangeForCharacterRange:NSMakeRange(charIndex,0)
actualCharacterRange:nil].location;
// If there is no valid character at the index we
must be at the beginning of the text view,
// and there must be no text in the text view
(because we adjust for this in textDidChange:)
if ([layoutManager isValidGlyphIndex:glyphIndex])
{
// First get the rect of the line the character is
in
r = [layoutManager
lineFragmentUsedRectForGlyphAtIndex:glyphIndex
effectiveRange:nil];
// Then get the place of the character in the line
r.origin.x = [layoutManager
locationForGlyphAtIndex:glyphIndex].x;
}
else
r = NSZeroRect; // No characters
// NEED to convert to allow for textContainer origin
here...
r = [self convertRect:r fromView:textView];
return r;
}
Many thanks to anyone who can see where I am going
wrong,
Keith
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
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