RE: Getting the rect of a character in an NSTextView
RE: Getting the rect of a character in an NSTextView
- Subject: RE: Getting the rect of a character in an NSTextView
- From: Keith Blount <email@hidden>
- Date: Mon, 24 Jan 2005 11:19:26 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
Sorry to reply to my own message, but I am still
completely stumped as to why the code below is causing
my app to crash when it the window is live resized
(actually it only crashes when resized to be smaller).
On futher testing, it seems that the crash is caused
when the charIndex passed in is high - over 50,000 or
so (actually not that high, really). I don't
understand why, though.
If anybody can see a flaw in this method I would be
very grateful. I am also thinking that I might be able
to optimise this view by only displaying the margin
notes that are on screen (in the clip view) at any one
time, though I'm not quite sure how I would go about
this...
Any help or tips on where I am going wrong would be
really appreciated.
Many thanks in advance,
Keith
-- Original post (problem code): --
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!?
Yahoo! Mail - Helps protect you from nasty viruses.
http://promotions.yahoo.com/new_mail
_______________________________________________
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