温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

弹出键盘textview

发布时间:2020-08-01 12:43:37 来源:网络 阅读:412 作者:Q一抹阳光Q 栏目:开发技术

- (void)setTextView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 528, 320, 40)];
    view.backgroundColor = [UIColor lightGrayColor];
    view.tag = 100;
    [self.view addSubview:view];
    [view release];
    
    UIImageView *p_w_picpathView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 30, 30)];
    p_w_picpathView1.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;//设置p_w_picpathView1的自适应高度,和父视图保持的下边距保持不变
    //p_w_picpathView1.p_w_picpath = [UIImage p_w_picpathNamed:@"bottleReceiverVoiceNodePlaying003@2x.png"];
    p_w_picpathView1.layer.cornerRadius = 15;
    p_w_picpathView1.clipsToBounds = YES;
    [view addSubview:p_w_picpathView1];
    [p_w_picpathView1 release];
    
    UIImageView *p_w_picpathView2 = [[UIImageView alloc] initWithFrame:CGRectMake(230, 0, 30, 30)];
    p_w_picpathView2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    //p_w_picpathView2.p_w_picpath = [UIImage p_w_picpathNamed:@"Album_ToolViewEmotionHL@2x.png"];
    p_w_picpathView2.layer.cornerRadius = 15;
    p_w_picpathView2.clipsToBounds = YES;
    [view addSubview:p_w_picpathView2];
    [p_w_picpathView2 release];

    UIImageView *p_w_picpathView3 = [[UIImageView alloc] initWithFrame:CGRectMake(280, 0, 30, 30)];
    p_w_picpathView3.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    //p_w_picpathView3.p_w_picpath = [UIImage p_w_picpathNamed:@"AlbumAddBtnHL@2x.png"];
    p_w_picpathView3.layer.cornerRadius = 15;
    p_w_picpathView3.clipsToBounds = YES;
    [view addSubview:p_w_picpathView3];
    [p_w_picpathView3 release];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 37, 200, 2)];
    label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    label.backgroundColor = [UIColor purpleColor];
    [view addSubview:label];
    [label release];
    
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(60, 0, 160, 36)];
    //autoresizing 自适应高度,是UIView的属性,可实现自动布局,
    //   self.textView.autoresizingMask = UIViewAutoresizingNone; //不会随父视图的改变而改变
    //   self.textView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; //自动调整view与父视图左边距,以保证右边距不变
    //    self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;//自动调整view自身的宽度,保证左边距和右边距不变
    //    self.textView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;//自动调整view与父视图右边距,以保证左边距不变
    //    self.textView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;//自动调整view与父视图上边距,以保证下边距不变
    //   self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自动调整view自身的高度,以保证上边距和下边距不变
    //    self.textView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;//自动调整view与父视图的下边距,以保证上边距不变
    textView.tag = 200;
    textView.scrollEnabled = YES;
    textView.editable = YES;
    textView.textAlignment = NSTextAlignmentLeft;
    textView.dataDetectorTypes = UIDataDetectorTypeAll;
    textView.delegate = self;
    textView.returnKeyType = UIReturnKeyDefault;
    textView.keyboardType = UIKeyboardTypeAlphabet;
    [view addSubview:textView];
    [textView release];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    return YES;
    
}
- (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary *userInfo = [notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat keyboardHeight = keyboardRect.size.height;
    
   
    /*
    UIView *view = [self.view viewWithTag:100];
    
    CGRect rect = view.frame;// 保存View原有的frame.
    rect.origin.y -= keyboardHeight;//y减去键盘的高度
    view.frame = rect;//把最新的frame赋值给view
    [UIView commitAnimations];
    */
    
   self.view.frame = CGRectMake(0, -keyboardHeight, 320, 568);
    
    
}

- (void)keyboardWillHide:(NSNotification *)notification {
    self.view.frame = CGRectMake(0, 0, 320, 568);

    /*
    NSDictionary *userInfo = [notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat keyboardHeight = keyboardRect.size.height;

    UIView *view = [self.view viewWithTag:100];
    
    CGRect rect = view.frame;// 保存View原有的frame.
    rect.origin.y += keyboardHeight;//y减去键盘的高度
    view.frame = rect;//把最新的frame赋值给view
    [UIView commitAnimations];
     */
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"%@",NSStringFromCGSize(textView.contentSize));
    if ([text isEqual:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
    //给输入框的高度设置限值为80
    if (textView.contentSize.height >= 80) {
        return;
    }
    textView.autoresizingMask = UIViewAutoresizingFlexibleHeight; //设置textView的自适应高度,改变自身高度,上下边框和父视图的间距保持不变
    UIView *view = (UIView *)[self.view viewWithTag:100];
    CGRect frame = view.frame;
    frame.size.height = textView.contentSize.height;
    frame.origin.y = 568 - textView.contentSize.height;
    view.frame = frame;

}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI