iOS 给TextView添加placeholder.

来源:互联网 发布:淘宝店铺排行榜怎么看 编辑:程序博客网 时间:2024/05/01 08:28

大家都知道textfield中有个属性可以直接设置placeholder,但是textview没有一个这样的属性,要想达到相同的效果只能采用在textview上添加label来实现了.

{

   UITextView * _inputTV;

    UILabel *_placeHolderLabel;

}



- (void)viewDidLoad {


    _inputTV = [[UITextViewalloc] initWithFrame:CGRectMake(15,10, kScreenWidth-30,130)];

    _inputTV.delegate =self;

    _inputTV.backgroundColor = [UIColorclearColor];

    _inputTV.textColor = [UIColorblackColor];

    [self.view addSubview:_inputTV];

    _inputTV.text =_model.descriptions;

    

    _placeHolderLabel = [[UILabelalloc] initWithFrame:CGRectMake(5,5,,20)];

    _placeHolderLabel.textAlignment =NSTextAlignmentLeft;

    _placeHolderLabel.font = [UIFontsystemFontOfSize:14];

    _placeHolderLabel.text =@"请输入内容";

    [_inputTVaddSubview:_placeHolderLabel];

}


- (void)textViewDidChange:(UITextView *)textView

{

    if (textView.text.length ==0 )

    {

        _placeHolderLabel.text =@"请输入内容";

    }

    else

    {

        _placeHolderLabel.text =@"";

    }

}


这样就达到了想要的效果.



0 0
原创粉丝点击