1.引入协议
<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.实现协议
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];//按回车取消第一相应者
}
return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
self.lb.alpha = 0;//开始编辑时
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{//将要停止编辑(不是第一响应者时)
if (textView.text.length == 0) {
self.lb.alpha = 1;
}
return YES;
}
网友评论