textview 占位字

 textview 和 textfield 不同,系统并没有自带占位字符,一种方法当然可以重写系统方法,但是可以采用一个假的方法来实现这个效果.还有计数功能哟!

1.首先创建 textview 注意: 要签订代理,设定代理人.记得把控件加在视图上.

- (UITextView *)remarkTextView {

    if (_remarkTextView == nil) {

        _remarkTextView = [[UITextView alloc] initWithFrame:CGRectMake(15, 15, SCREEN_WIDTH - 30, 100)];

        _remarkTextView.text = @" 请输入您的需求...";

        _remarkTextView.textColor[UIColor grayColor];

        _remarkTextView.font = [UIFont systemFontOfSize:15];

        _remarkTextView.layer.borderWidth = 0.5f;

        _remarkTextView.layer.cornerRadius = 4.0f;

        _remarkTextView.delegate = self;

    }

    return _remarkTextView;

}


- (UILabel *)numLabel {

    if (_numLabel == nil) {

        _numLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 120 , SCREEN_WIDTH - 10, 20)];

        _numLabel.textAlignment = NSTextAlignmentRight;

        _numLabel.text = @"0/200";

        _numLabel.textColor = [UIColor grayColor];

        _numLabel.font = [UIFont systemFontOfSize:14];

    }

    return _numLabel;

}



#pragma mark textview 代理

- (void)textViewDidChange:(UITextView *)textView {

    if (self.remarkTextView.text.length > 200) {

        NSString *subString = [self.remarkTextView.text substringToIndex:200];

        self.remarkTextView.text = subString;

        NSLog(@"限制输入200");

    }

    self.remarkTextView.textColor = [UIColor blackColor];

    self.numLabel.text = [NSString stringWithFormat:@"%ld/200",(unsigned long)textView.text.length];

}


- (void)textViewDidBeginEditing:(UITextView *)textView {

    if ([textView.text isEqualToString:@" 请输入您的需求..."]) {

        textView.text = @"";

    }

}


- (void)textViewDidEndEditing:(UITextView *)textView {

    if (textView.text.length < 1) {

        textView.text = @" 请输入您的需求...";

        textView.textColor = [UIColor grayColor];

    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值