1

I want to limit my textview to 100 characters for instance, what is the simplest way to accomplish that?

this is that view controller that contains the textview:

#import "CreatePageViewController.h"
#import "PopUpView.h"

@interface CreatePageViewController ()

@property (weak, nonatomic) IBOutlet UITextView *myTextView;

@end

@implementation CreatePageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(IBAction)close:(id)sender {

    [[NSNotificationCenter defaultCenter]postNotificationName:@"HideAFPopup" object:nil];
}

-(void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor clearColor];
    UIToolbar *blurbar = [[UIToolbar alloc] initWithFrame:self.view.frame];
    blurbar.barStyle = UIBarStyleDefault;
    [self.view addSubview:blurbar];
    [self.view sendSubviewToBack:blurbar];

    // Subscribe to keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

#pragma mark - UIKeyboard

- (void)keyboardWillShow:(NSNotification *)notification
{
    if ([self.myTextView isFirstResponder]) {
        NSDictionary *info = [notification userInfo];
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

        [UIView animateWithDuration:duration
                         animations:^{
                             self.myTextView.contentInset = UIEdgeInsetsMake(self.myTextView.contentInset.top, self.myTextView.contentInset.left, kbSize.height, 0);
                             self.myTextView.scrollIndicatorInsets = UIEdgeInsetsMake(self.myTextView.contentInset.top, self.myTextView.scrollIndicatorInsets.left, kbSize.height, 0);
                         }];
    }
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    if ([self.myTextView isFirstResponder]) {
        NSDictionary *info = [notification userInfo];
        CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

        [UIView animateWithDuration:duration
                         animations:^{
                             self.myTextView.contentInset = UIEdgeInsetsMake(self.myTextView.contentInset.top, self.myTextView.contentInset.left, 0, 0);
                             self.myTextView.scrollIndicatorInsets = UIEdgeInsetsMake(self.myTextView.contentInset.top, self.myTextView.scrollIndicatorInsets.left, 0, 0);
                         }];
    }
}

Please help me understand how to do that...thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
JohnBigs
  • 2,691
  • 3
  • 31
  • 61
  • This has been answered here: http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield – user3602554 Nov 22 '14 at 15:03
  • 1
    The duplicate is about a `UITextField` but the solution is basically the same for `UITextView`. Just use the appropriate `UITextViewDelegate` method instead of the `UITextFieldDelegate` method. – rmaddy Nov 22 '14 at 16:23

0 Answers0