iOS UITextView占位符

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.實現(xiàn)協(xié)議

- (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;

}

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容