博客> iOS-UITextView设置PlaceHolder
iOS-UITextView设置PlaceHolder
2018-10-18 20:28 评论:0 阅读:744 OS_JJ
ios iOS-UITextView设置PlaceHolder
1.声明属性和代理
    <UITextViewDelegate>
    @property(nonatomic,strong)UILabel *PLlab;
    @property(nonatomic,strong)UITextView *inputView;

2.创建textview和lable
     //输入框视图
    inputView = [[UITextView alloc]initWithFrame:CGRectMake(5, 72, WIDTH-10, (HEIGHT-70)/3-20)];
    inputView.keyboardType = UIKeyboardTypeDefault;
    inputView.font = [UIFont systemFontOfSize:14];
    inputView.delegate = self;
    inputView.textColor = TEXTCOLOR;
    [self.view addSubview:inputView];

    self.PLlab = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, WIDTH, 40)];
    self.PLlab.text = @"您此刻的想法,最多1500字";
    self.PLlab.font = [UIFont systemFontOfSize:14];
    self.PLlab.textColor = TEXTTINTCOLOR;
    [inputView addSubview:self.PLlab];

3.开始编辑时调用相应方法隐藏lable
    - (void)textViewDidBeginEditing:(UITextView *)textView{
        self.PLlab.hidden = YES;
}

4.点击view缩回键盘时显示lable
    - (void)touchesBegan:(NSSet<UITouch> *)touches withEvent:(UIEvent *)event{
        [self.view endEditing:YES];
       if (inputView.text.length > 0) {
            self.PLlab.hidden = YES;
        }else{
            self.PLlab.hidden =  NO;
}
}
收藏
0
sina weixin mail 回到顶部