UITextView添加占位符

- (void)createSubView:(CGRect)frame
{


    self.backgroundColor = kGrayLightColor;
    _textView = [CommonMethod createTextView:_textView whoViewAdd:self frame:CGRectMake(15, 5, frame.size.width-30, /*frame.size.height-110-30-20*/35) placehold:@""];


    self.placeHolderTextViewLabel = [[UILabel alloc] initWithFrame:CGRectMake(7, 6, 400/2, 20)];
    [self addPlaceholdWithTextView:_textView placehold:self.placeHolderTextViewLabel text:@"以上均无,请手动输入"];
}

#pragma mark - 添加占位符方法
- (void)addPlaceholdWithTextView:(UITextView *)textView placehold:(UILabel *)placehold text:(NSString *)text
{

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged1:) name:UITextViewTextDidChangeNotification object:nil];

    placehold.lineBreakMode = NSLineBreakByWordWrapping;
    placehold.font = [UIFont systemFontOfSize:14.0f];
    placehold.textColor = [UIColor grayColor];
    placehold.backgroundColor = [UIColor clearColor];
    placehold.alpha = 0;
    placehold.tag = 999;
    placehold.text = text;
    [textView addSubview:placehold];

    if ([[self.textView text] length] == 0) {
        [placehold setAlpha:1];
    }
}

#pragma mark - 添加占位通知方法 输入框要编辑的时候
- (void)textChanged1:(NSNotification *)notification
{
    //NSLog(@"notification == %@", notification);

    if ([[self.textView text] length] == 0) {
        [self.placeHolderTextViewLabel setAlpha:1];
    }
    else {
        [self.placeHolderTextViewLabel  setAlpha:0];
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值