Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 17, 2007, 01:01 AM   #1
RD92
macrumors newbie
 
Join Date: Jul 2007
Continuously check if...

Hi, I created an app that counts the lines in a text view. 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?
RD92 is offline   0 Reply With Quote
Old Jul 17, 2007, 01:55 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
You don't want to continuously check for changes: this will use a large amount of CPU (this solution is called polling and, in general, is something we try and avoid).

The better solution would be to have the text view tell you when there is a change and re-count then.

To do this set your instance as the delegate of the view. You then have a number of methods you could implement.

The textDidChange: method of NSText would work, although from memory this would only get called when editing ends (i.e. you select another control) so is probably not what you want.

A better method might be to implement - (BOOL)textViewNSTextView *)aTextView shouldChangeTextInRangeNSRange)affectedCharRange replacementStringNSString *)replacementString. Remember to return YES from this! This should get called on all changes to the text.
robbieduncan is offline   0 Reply With Quote
Old Jul 17, 2007, 03:31 AM   #3
RD92
Thread Starter
macrumors newbie
 
Join Date: Jul 2007
Ok thanks.

Last edited by RD92; Jul 17, 2007 at 03:43 AM.
RD92 is offline   0 Reply With Quote
Old Jul 17, 2007, 04:46 AM   #4
Nutter
macrumors 6502
 
Join Date: Mar 2005
Location: London, England
Also, a for loop isn't particularly suitable here. Try this:

Code:
unsigned numberOfLines = 0;
unsigned index = 0;
while (index < [[textView string] length])
{
	index = NSMaxRange([[textView string] lineRangeForRange:NSMakeRange(index, 0)]);
	++numberOfLines;
}
[textField setIntValue:numberOfLines];
(I know this wasn't part of your question, so sorry if this is unwelcome advice.)
Nutter is offline   0 Reply With Quote
Old Jul 17, 2007, 05:09 AM   #5
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
@RD I think you are wrong, I think textDidChange does get called every time you type a character, at least certainly one of the delegates of NSText or NSTextField does. I think textDidEndEditing only gets called when you end editing .


I'd love to help find which method but I changed my code to use bindings instead.
Eraserhead is offline   0 Reply With Quote
Old Jul 17, 2007, 05:26 AM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by Eraserhead View Post
@RD I think you are wrong, I think textDidChange does get called every time you type a character, at least certainly one of the delegates of NSText or NSTextField does. I think textDidEndEditing only gets called when you end editing .
In that case that would be great. I have distinct memories of it not being called on very character change in an NSTextField though. I could be thinking of another delegate method though.

The simplest thing would be to try it!

Another reason for suggesting the other method was that it tells you the range being effected and the new characters. So you could use this data to update the count without a full recount which could save a lot of CPU time on a very long document...
robbieduncan is offline   0 Reply With Quote
Old Jul 17, 2007, 07:39 AM   #7
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Quote:
Originally Posted by robbieduncan View Post
Another reason for suggesting the other method was that it tells you the range being effected and the new characters. So you could use this data to update the count without a full recount which could save a lot of CPU time on a very long document...
True, it did seem to be very inefficient to do it my way.
Eraserhead is offline   0 Reply With Quote
Old Jul 17, 2007, 08:32 AM   #8
RD92
Thread Starter
macrumors newbie
 
Join Date: Jul 2007
Ok, I switched to textDidChange:NSTextDidEndEditingNotification but I have no idea how to count only the characters that have been added. Here's my code:

Code:
- (void)textDidChange:NSTextDidEndEditingNotification
{
	linesString = [textView string];
	unsigned numberOfLines;
	unsigned index;
	unsigned stringLength = [linesString length];
	for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++)
	{
		index = NSMaxRange([linesString lineRangeForRange:NSMakeRange(index, 0)]);
		[linesField setIntValue:numberOfLines + 1];
	}
}
RD92 is offline   0 Reply With Quote
Old Jul 17, 2007, 08:41 AM   #9
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
There's no need to update your text field every single time. Just do it once at the end. Also, isn't componentsSeparatedByString: a more simpler way to count the # of lines?

Edit: Use Nutter's code. Much easier to read and more efficient.
kainjow is offline   0 Reply With Quote
Old Jul 17, 2007, 09:25 AM   #10
RD92
Thread Starter
macrumors newbie
 
Join Date: Jul 2007
Ok, thanks it worked perfectly. Just one more thing, how can I get the current line that the insertion point is in?
RD92 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
SSD continuously shows less and less space joejoejoe MacBook Air 6 Nov 30, 2013 03:04 PM
Continuously saying Haha The Pearl Wasteland 6 Sep 23, 2013 04:55 AM
Computer Continuously Turning On Jonnygill Community Discussion 0 Apr 6, 2013 09:23 PM
Continuously Play Podcasts? ubhidoobeedoo MacBook Pro 2 Feb 18, 2013 11:51 PM
Itunes not continuously playing J3ayy Mac Applications and Mac App Store 1 Sep 24, 2012 08:26 AM

Forum Jump

All times are GMT -5. The time now is 12:39 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC