如何将textview限制为x个字符?

时间:2014-11-22 13:56:37

标签: ios objective-c uitextview

我想将textview限制为100个字符,例如,最简单的方法是什么?

这是包含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);
                         }];
    }
}

请帮助我了解如何做到这一点......谢谢

0 个答案:

没有答案