美文网首页
iOS-UITextView设置PlaceHolder

iOS-UITextView设置PlaceHolder

作者: Mn_Su | 来源:发表于2016-09-23 13:16 被阅读0次
    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.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<UITouch> *)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

          本文链接:https://www.haomeiwen.com/subject/msutyttx.html