在textView上添加placeholder,思路是在要编辑的UITextView底部再放一个UITextView,并且将前面的背景色设为透明,代码如下:
_placeholder = [[UITextView alloc] initWithFrame:self.frame];
[_placeholder setEditable:NO];
_placeholder.text = @"请输入...";
[self addSubview:_placeholder];
UITextView *textView = [[UITextView alloc] initWithFrame:self.frame];
textView.layer.cornerRadius = 10;
textView.layer.borderColor = [[UIColor grayColor] CGColor];
textView.layer.borderWidth = 1;
textView.backgroundColor = [UIColor clearColor];
[self addSubview:textView];
textView.delegate = self;- (void)textViewDidChange:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
_placeholder.hidden = NO;
}
else
{
_placeholder.hidden = YES;
}
}
本文详细介绍了如何在TextView上添加placeholder的功能,通过在要编辑的UITextView底部添加一个透明背景的UITextView来实现。包括代码示例、设置透明背景、圆角边框、文本颜色等操作。

598

被折叠的 条评论
为什么被折叠?



