Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
- NSLog(@"%d",range.length);
- if (range.length == 1) {
- NSLog(@"Backspace");
- charactersLeft++;
- if (characterCountLabel.text.intValue >= 30)
- characterCountLabel.textColor = [UIColor grayColor];
- } else if (range.length == 0) {
- if (characterCountLabel.text.intValue <= 30) {
- characterCountLabel.textColor = [UIColor redColor];
- }
- if (charactersLeft == 0) {
- charactersLeft = 0;
- }
- else {
- if ([text isEqualToString:@""] && [textView.text isEqualToString:@""]) {
- NSLog(@"Empty field backspace");
- return NO;
- }
- NSLog(@"You added a character");
- charactersLeft--;
- }
- }
- characterCountLabel.text = [NSString stringWithFormat:@"%d",charactersLeft];
- NSLog(@"%d",charactersLeft);
- NSUInteger newLength = (textView.text.length - range.length) + text.length;
- if(newLength <= characterLimit)
- {
- NSLog(@"Char");
- NSLog(@"%u",newLength);
- characterCountLabel.text = [NSString stringWithFormat:@"%d",charactersLeft];
- return YES;
- } else {
- NSLog(@"End");
- characterCountLabel.text = [NSString stringWithFormat:@"%d",charactersLeft];
- NSUInteger emptySpace = characterLimit - (textView.text.length - range.length);
- textView.text = [[[textView.text substringToIndex:range.location]
- stringByAppendingString:[text substringToIndex:emptySpace]]
- stringByAppendingString:[textView.text substringFromIndex:(range.location + range.length)]];
- return NO;
- }
- return YES;
- }
Advertisement
Add Comment
Please, Sign In to add comment