iOS Object-C UITextview 监听文本改动

1.#pragma mark Lazy ------------ 懒加载创建UITextview
 - (UITextview *)viewText {
    if (!_viewText) {
        _viewText =[[UITextview alloc]init];
        _viewText.text = @"textView";
        _viewText.font = kSystemFont(15);
        _viewText.textColor = COLOR_WHITE;
        [_viewText setShowsVerticalScrollIndicator:NO];
        [_viewText setShowsHorizontalScrollIndicator:NO];
        _viewText.delegate = self;
        _viewText.backgroundColor = COLOR_SYSTEM_VIEW_BACKGROUND;
    }
    return _viewText;
}

2. #pragma mark  initTextView ------------ add to self.view   and  regist  NSNotificationCenter
- (void)initTextView {
    [self.view addSubview:self.viewText];
    [self.viewText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_bottom);
        make.bottom.equalTo(self.view.mas_top);
        make.width.equalTo(self.view);
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEditeAction:)name:UITextViewTextDidChangeNotification object:nil];

3. #pragma mark Action ------------ textViewEditeAction 监听文本被修改 
- (void)textViewEditeAction:(UITextView *)sender {
    NSLog(@"sender %@ ",self.viewText.selectedRange.location);
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

你可能感兴趣的:(iOS Object-C UITextview 监听文本改动)