Re: Getting the current line
Re: Getting the current line
- Subject: Re: Getting the current line
- From: Seth Willits <email@hidden>
- Date: Thu, 23 Aug 2007 12:00:36 -0700
On Aug 23, 2007, at 1:47 AM, Ricardo Diaz wrote:
How can I get the current line the insertion point is in? I tried
modifying this code that gets the current column but got nothing:
NSRange selectionRange = [textView selectedRange];
unsigned int selection = selectionRange.location;
unsigned int end = [[textView string]
lineRangeForRange:selectionRange].location-1;
unsigned int currentCol = (selection-end);
Any ideas?
You'll need to count the number of line endings from the beginning to
your current position. If you subclass NSTextStorage and override the
replaceCharacters:... method you can intercept all messages and build
and accurate mapping of line endings to character positions pretty
easily. Each time a portion of the storage is changed, invalidate
from that location on. Then when you ask for a line number, if it's
character index is invalid, you scan from the last known valid
location up to the desired line number, inserting new valid entries
as you go.
It sounds hard and slow, but it's really not, even in a very large
documents.
--
Seth Willits
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden