I am trying to implement a character counter for a UITextView, but have found the counter to be inaccurate in the following situation:
- Enter a few characters of text, such as "The"
- Tap return 25 times
- Hold down the delete button to delete the text (and hold it down until the cursor gets to the top)
- When the cursor reaches the top (and after deleting "The"), the counter still says the UITextView contains 3 characters
- Tap the delete button once more to have the count correct itself and say there are 0 characters
I've outputted the results of the counter and the replacementText in this situation and the last output is as follows:
2009-08-06 15:29:14.357 Characters: 3
2009-08-06 15:29:14.369 The
Therefore, it appears that when a large number of newlines (and perhaps large portions of text in general) are deleted en masse in this manner, textView:shouldChangeTextInRange:replacementText: is not fired at the end of that sequence, requiring the addition tap of the delete button.
My counter code is as follows:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSString* nextText = [textView.text stringByReplacingCharactersInRange:range withString:text];
int characterCount = [nextText length];
Any ideas on how to overcome this so the count is correct in this situation (or what I may be doing wrong)?