一、UITextView
1. 在@interface中声明代理
@interface MyViewController : UIViewController <UITextViewDelegate>
2. 设置UITexView的delegate,可以在viewDidLoad方法中实现,也可以在nib文件(或是storyboard)中实现。
textView.delegate = self;
3. 实现代理方法。
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)textreturn NO;
}else{ if ([text isEqualToString:@"\n"])
{
[self fankui];return NO;
}二、UITextField
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.titleTextField) {
NSString *string0 = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([string0 length] > 30)
{
string0 = [string0 substringToIndex:30];
self.titleTextField.text = string0;
self.titleNumLabel.text = [NSString stringWithFormat:@"%d/30",string0.length];
self.titleNumLabel.textColor = kUIColorFromRGB(0xb7babb);
return NO;
}else{
self.titleNumLabel.text = [NSString stringWithFormat:@"%d/30",string0.length];
if (string0.length < 6 || string0.length > 30) {
self.titleNumLabel.textColor = [UIColor redColor];
}else{
self.titleNumLabel.textColor = kUIColorFromRGB(0xb7babb);
}
}
}
return YES;
}