第一步:在Xib中拖一個TextView和placeholder(Label)atom
第二步:上代碼->code
@property (weak, nonatomic) IBOutlet UITextView *remarkTextView; @property (weak, nonatomic) IBOutlet UILabel *placeholder; - (void)viewDidLoad { self.remarkTextView.delegate =self; self.placeLabel.enabled = NO; } //UITextViewDelegate - (void)textViewDidChange:(UITextView *)textView { NSInteger number = [textView.text length]; if (number>0) { _placeholder.hidden = YES; }else{ _placeholder.hidden = NO; } //限制輸入的字數並統計輸入的字數 if (number > 50) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"舒適提示" message:@"字符個數不能大於50" delegate:self cancelButtonTitle:@"肯定"otherButtonTitles:nil]; [alert show]; textView.text = [textView.text substringToIndex:50]; number = 50; } self.textNum.text = [NSString stringWithFormat:@"%ld/50", (long)number]; }
這個效果跟咱們UITextField的placeholder就如出一轍啦~
orm