Detecting when the number of lines in an NSTextView has changed
Detecting when the number of lines in an NSTextView has changed
- Subject: Detecting when the number of lines in an NSTextView has changed
- From: Keith Blount <email@hidden>
- Date: Sat, 19 Feb 2005 04:28:10 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
Hello,
I am currently trying to optimise a view I have been
working on which has margin notes aligned next to text
in an NSTextView. Currently, whenever the text in the
text view changes, I calculate the position of the
characters to which any notes are attached in the
layout manager and then move the margin notes
accordingly.
So far, so good, but there is some slowdown if there
are lots of margin cards and the user types at the top
of the text (because all of the margin cards below
that place in the text have to have there position
recalculated whenever the user types anything).
What I would like to do is only calculate new
positions for the margin cards if the number of lines
in the text view changes - this way there would only
ever be slowdown when the user hits return or typing
spills over onto another line.
Is there a quick and efficient way of doign this? I
tried checking [[textView textStorage] size].height in
textView:shouldChangeTextInRange... and then comparing
it to the new height in textDidChange, but it seems
that this method is really expensive, as it caused far
more slowdown than my original technique.
I was also thinking of adapting the following code
from the docs:
NSLayoutManager *layoutManager = [textView
layoutManager];
unsigned numberOfLines, index, numberOfGlyphs =
[layoutManager numberOfGlyphs];
NSRange lineRange;
for (numberOfLines = 0, index = 0; index <
numberOfGlyphs; numberOfLines++){
(void) [layoutManager
lineFragmentRectForGlyphAtIndex:index
effectiveRange:&lineRange];
index = NSMaxRange(lineRange);
}
which calculates the number of wrapped lines, so that
it only counts the number of lines affected before and
after an edit, but again, this would be slow because
it is lineFragmentRectForGlyphAtIndex: which is
causing the slowdown in my view at the moment.
In short, does anybody know of a really inexpensive
way of checking that a line has been added/removed
from a text view?
Many thanks in advance for any help,
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