Guest User

Untitled

a guest
Aug 10th, 2012
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  2.    
  3.     NSLog(@"%d",range.length);
  4.    
  5.     if (range.length == 1) {
  6.         NSLog(@"Backspace");
  7.         charactersLeft++;
  8.        
  9.         if (characterCountLabel.text.intValue >= 30)
  10.             characterCountLabel.textColor = [UIColor grayColor];
  11.  
  12.     } else if (range.length == 0) {
  13.  
  14.         if (characterCountLabel.text.intValue <= 30) {
  15.             characterCountLabel.textColor = [UIColor redColor];
  16.         }
  17.        
  18.         if (charactersLeft == 0) {
  19.             charactersLeft = 0;
  20.         }
  21.         else {
  22.             if ([text isEqualToString:@""] && [textView.text isEqualToString:@""]) {
  23.                 NSLog(@"Empty field backspace");
  24.                 return NO;
  25.             }
  26.                 NSLog(@"You added a character");
  27.             charactersLeft--;
  28.         }
  29.     }
  30.    
  31.     characterCountLabel.text = [NSString stringWithFormat:@"%d",charactersLeft];
  32.     NSLog(@"%d",charactersLeft);
  33.  
  34.     NSUInteger newLength = (textView.text.length - range.length) + text.length;
  35.     if(newLength <= characterLimit)
  36.     {
  37.         NSLog(@"Char");
  38.         NSLog(@"%u",newLength);
  39.         characterCountLabel.text = [NSString stringWithFormat:@"%d",charactersLeft];
  40.         return YES;
  41.     } else {
  42.         NSLog(@"End");
  43.         characterCountLabel.text = [NSString stringWithFormat:@"%d",charactersLeft];
  44.         NSUInteger emptySpace = characterLimit - (textView.text.length - range.length);
  45.         textView.text = [[[textView.text substringToIndex:range.location]
  46.                           stringByAppendingString:[text substringToIndex:emptySpace]]
  47.                          stringByAppendingString:[textView.text substringFromIndex:(range.location + range.length)]];
  48.         return NO;
  49.     }
  50.    
  51.     return YES;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment