UITextView和UITextField的placeholder,键盘隐藏,键盘换行变完成字样

本文转载至 

{

    //设置页面的背景颜色

    UIColor *ViewBgColor = [UIColor colorWithRed:(247.0f/255.0f)green:(247.0f/255.0f) blue:(247.0f/255.0f) alpha:1.0f];

    self.view.backgroundColor = ViewBgColor;

    

    UILabel *fix_feed_label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 70, 30)];

    fix_feed_label.text = @"意见内容";

    fix_feed_label.backgroundColor = [UIColor clearColor];

    fix_feed_label.textColor = [UIColor blackColor];

    

    _content_textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 25, 300, 80)];

    _content_textView.font =  [UIFont boldSystemFontOfSize:13];

    _content_textView.delegate = self;

    //设置键盘,使换行变为完成字样

    _content_textView.keyboardType = UIKeyboardAppearanceDefault;

    _content_textView.returnKeyType = UIReturnKeyDone;

    

    _placeholder_label = [[UILabel alloc]initWithFrame:CGRectMake(12, 25, 300, 30)];

    _placeholder_label.text = @"请留下您的宝贵意见或建议(不少于10个汉字)";

    _placeholder_label.font =  [UIFont boldSystemFontOfSize:13];

    _placeholder_label.textColor = [UIColor lightGrayColor];

    _placeholder_label.layer.cornerRadius = 10;

    _placeholder_label.layer.masksToBounds = YES;

    

    UILabel *fix_contact_label = [[UILabel alloc]initWithFrame:CGRectMake(10, 105, 160, 30)];

    fix_contact_label.text = @"联系方式 (选填)";

    fix_contact_label.backgroundColor = [UIColor clearColor];

    fix_contact_label.textColor = [UIColor blackColor];

    

    _contact_field = [[UITextField alloc]initWithFrame:CGRectMake(15, 130, 300, 30)];

    [_contact_field setBorderStyle:UITextBorderStyleNone];

    _contact_field.font =  [UIFont boldSystemFontOfSize:13];

    _contact_field.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;

    _contact_field.keyboardType = UIKeyboardAppearanceDefault;

    _contact_field.returnKeyType = UIReturnKeyDone;

    _contact_field.placeholder = @"手机号码或EMAIL";

    _contact_field.delegate = self;

    

    _submit_button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    [_submit_button setFrame: CGRectMake(0, 0, 55, 27)];

    UIColor *sequenceColor = [UIColor colorWithRed:(246.0f/255)green:(109.0f/255.0f) blue:(9.0f/255.0f) alpha:1.0f];

    _submit_button.backgroundColor = sequenceColor;

    [_submit_button setTitle:@"提交"forState:UIControlStateNormal];

    [_submit_button setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];

    [_submit_button addTarget:self action:@selector(clickSubmit:)forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:_submit_button];

    

    

    [self.view addSubview:fix_feed_label];

    [self.view addSubview:fix_contact_label];

    [self.view addSubview:_content_textView];

    [self.view addSubview:_placeholder_label];

    [self.view addSubview:_contact_field];

}

 

- (void)clickSubmit:(id)sender

{

    NSLog(@"clickSubmit");

}

 

/*

 基于UIView点击编辑框以外的虚拟键盘收起

 **/

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    if (![self.content_textView isExclusiveTouch]||(![self.contact_field isExclusiveTouch])) {

        if (self.content_textView.text.length == 0)

        {

            NSLog(@"ssssss");

            self.placeholder_label.text = @"请留下您的宝贵意见或建议(不少于10个汉字)";

            _placeholder_label.hidden = NO;

        }

        [self.content_textView resignFirstResponder];

        [self.contact_field resignFirstResponder];

    }

}

 

/*

 键盘收回事件,UITextField协议方法

 **/

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    return NO;

}

 

#pragma textViewDelegate

-(void)textViewDidChange:(UITextView *)textView

{

      

    if (self.content_textView.text.length != 0) {

        self.placeholder_label.text = @"";

        _placeholder_label.hidden = YES;

    }

    else{

        self.placeholder_label.text = @"请留下您的宝贵意见或建议(不少于10个汉字)";

        _placeholder_label.hidden = NO;

    }

}

 

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

{

    self.placeholder_label.text = @"";

}

 

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{

    if ([@"\n" isEqualToString:text] == YES)

    {

        [textView resignFirstResponder];

        if (self.content_textView.text.length == 0)

        {

            NSLog(@"ssssss");

            self.placeholder_label.text = @"请留下您的宝贵意见或建议(不少于10个汉字)";

            _placeholder_label.hidden = NO;

        }

        return NO;

    }

    

    return YES;

}

转载于:https://www.cnblogs.com/Camier-myNiuer/p/4600351.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值