要做字数限制,网上看了一些,都不是很好用,要不就是不能同时兼顾ios6,7 。在textViewDidChange:事件进行添加
- (void)textViewDidChange:(UITextView *)textView
{
if(![textView markedTextRange])
{
if(textView.text.length > 20)
{
UIAlertView *alter_view = [[UIAlertView alloc] initWithTitle:@"内容过长" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter_view show];
textView.text = [textView.text substringToIndex:20];
}
NSLog(@"%@",[NSString stringWithFormat:@"%d/20",textView.text.length]);
//_label_textCount.text = [NSString stringWithFormat:@"%d/20",textView.text.length];
}
}
另外附上点击回收键盘
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"] && ![textView markedTextRange])
{
[textView resignFirstResponder];
return NO;
}
return YES;
}