• 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: Getting the current line
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Getting the current line (From: Ricardo Diaz <email@hidden>)

  • Prev by Date: Uppercasing text as it is entered
  • Next by Date: Re: Uppercasing text as it is entered
  • Previous by thread: Getting the current line
  • Next by thread: Hillegass cocoa question
  • Index(es):
    • Date
    • Thread