iOS-UITextView设置PlaceHolder

1.声明属性和代理
    
    @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.inputView setValue:[UIColor colorWithHex:0xbababa] forKeyPath:@"_placeholderLabel.textColor"];
// [self.inputView setValue:TEXTFONT(16) forKeyPath:@"_placeholderLabel.font"];

    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 *)touches withEvent:(UIEvent *)event{
        [self.view endEditing:YES];
       if (inputView.text.length > 0) {
            self.PLlab.hidden = YES;
        }else{
            self.PLlab.hidden =  NO;
}
}

你可能感兴趣的:(iOS-UITextView设置PlaceHolder)