1.引入?yún)f(xié)議
<UITextViewDelegate>
2.添加屬性
@property (nonatomic,strong)UITextView *otherField;
//占位符
@property (nonatomic, strong)UILabel *lb;
3.懶加載
- (UITextView *)otherField
{
if (!_otherField) {
_otherField = [[UITextView alloc]initWithFrame:[WPackageViewController createFrimeWithX:10 andY:276 andWidth:355 andHeight:139]];
_otherField.font = [UIFont systemFontOfSize:15*W];
_otherField.layer.borderColor = [[UIColor lightGrayColor]CGColor];//陰影
_otherField.layer.borderWidth = 1;
_otherField.layer.cornerRadius = 5*W;
_otherField.clipsToBounds = YES;
_otherField.delegate = self;
}
return _otherField;
}
- (UILabel *)lb
{
if (!_lb) {
_lb = [[UILabel alloc]initWithFrame:[WPackageViewController createFrimeWithX:10 andY:10 andWidth:200 andHeight:20]];
_lb.text = @"請輸入其他原因...";
_lb.textColor = [UIColor grayColor];
_lb.font = [UIFont systemFontOfSize:15*W];
}
return _lb;
}
4.加載
[self.otherField addSubview:self.lb];
5.實(shí)現(xiàn)協(xié)議
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];//按回車取消第一相應(yīng)者
}
return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
self.lb.alpha = 0;//開始編輯時(shí)
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{//將要停止編輯(不是第一響應(yīng)者時(shí))
if (textView.text.length == 0) {
self.lb.alpha = 1;
}
return YES;
}