Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Counting the lines of NSTextView

Counting the lines of NSTextView
Thread Tools
RDG
Fresh-Faced Recruit
Join Date: Feb 2007
Status: Offline
Reply With Quote
Jul 16, 2007, 04:04 PM
 
Hi, is there a method that returns the number of lines in an NSTextView? Something like:

[textView numberOfLines];

If not, how can I do this?

Thanks in advanced
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 16, 2007, 05:28 PM
 
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
RDG  (op)
Fresh-Faced Recruit
Join Date: Feb 2007
Status: Offline
Reply With Quote
Jul 17, 2007, 12:31 AM
 
Ok, so I got it to work like this:

- (void)awakeFromNib
{
string = [textView string];
unsigned numberOfLines;
unsigned index;
unsigned stringLength = [string length];
for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++)
{
index = NSMaxRange([string lineRangeForRange:NSMakeRange(index, 0)]);
[textField setIntValue:numberOfLines + 1];
}
}

I put '+ 1' because, I don't know why, it starts from zero. It works perfectly except It just counts them once. If you modify the text view, the text field doesn't change. How can I make it to coninuously check for changes in the text view?

Thanks for your help.
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 17, 2007, 06:06 PM
 
The simplest way would be to give the text view a delegate whose textDidChange: method runs that same code again to update your line count. This may or may not be fast enough for your purposes — I think it's basically O(n) for the number of characters in your document, and it will be called literally every time the user does anything in the text view.

By the way, in C-based languages, ranges almost always start at 0 (e.g., the first item in an array is at index 0).
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:30 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2