textView字数限制问题

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(originX, originY, width, height)];

textView.backgroundColor = [UIColor whiteColor];

textView.font = [UIFont systemFontOfSize:12];

textView.placeholderColor = [UIColor redColor];

textView.placeholderLabel.font = [UIFont systemFontOfSize:12];

textView.textColor = [UIColor redColor];

textView.placeholder = @"请输入...";

textView.delegate = self;

[self.view addSubview:textView];

//设置textview是否可编辑

@property (assign, nonatomic) BOOL isContentTextViewEnable;

#pragma mark textview delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

if ([text isEqualToString:@""]) {

return YES;

}else{

return self.isContentTextViewEnable;

}

}

- (void)textViewDidChange:(UITextView *)textView {

NSDictionary *attibute = @{

@"body":@[[UIFont systemFontOfSize:11],[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]],

@"num":[UIColor colorWithRed:255/255.0 green:80/255.0 blue:11/255.0 alpha:1.0f]

};

self.isContentTextViewEnable = true;

//获取当前键盘类型

UITextInputMode *mode = (UITextInputMode *)[UITextInputMode activeInputModes][0];

//获取当前键盘语言

NSString *lang = mode.primaryLanguage;

//如果语言是汉语(拼音)

if ([lang isEqualToString:@"zh-Hans"]) {

//取到高亮部分范围

UITextRange *selectedRange = [self.clubIntroTextView markedTextRange];

//取到高亮部分

UITextPosition *position = [self.clubIntroTextView positionFromPosition:selectedRange.start offset:0];

//如果取不到高亮部分,代表没有拼音

if (!position){

//当超过最大字数限制时

if (self.clubIntroTextView.text.length > 48) {

//对超出部分进行裁剪

self.clubIntroTextView.text = [self.clubIntroTextView.text substringToIndex:48];

//同时将可继续书写属性设为否,shouldChangeTextInRange方法会调用

self.isContentTextViewEnable = NO;           

}

}else {

//表示还有高亮部分,暂不处理

}

}else {

//如果语言不是汉语,直接计算

if (self.clubIntroTextView.text.length > 48) {

self.clubIntroTextView.text = [self.clubIntroTextView.text substringToIndex:48];

self.isContentTextViewEnable = NO;

}

}

}

//如果需要配合键盘移动,不需要忽略

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

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

//监听textfield输入文字的个数

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:)

name:@"UITextFieldTextDidChangeNotification" object:self.clubNameTextField];

}

- (void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

#pragma mark 键盘的通知

- (void)keyboardWillShow:(NSNotification *)noti {

NSValue *frameValue = [dict valueForKey:UIKeyboardFrameEndUserInfoKey];

CGRect frame = [frameValue CGRectValue];

//获取弹键盘前的偏移量

self.offsetY = self.tableView.contentOffset.y;

CGFloat height = frame.size.height;

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, height, 0);

//将tableview滚动到textfield对应的行

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:7 inSection:0]  atScrollPosition:UITableViewScrollPositionBottom animated:NO];//这里一定要设置为NO,动画可能会影响到scrollerView,导致增加数据源之后,tableView到处乱跳

}

- (void)keyboardWillHide:(NSNotification *)noti {

self.tableView.contentInset = UIEdgeInsetsZero;

}

你可能感兴趣的:(textView字数限制问题)