使用DAKeyboardControl更改uikeyboard上方的uitextview的动态高度

时间:2014-04-08 09:57:28

标签: ios uitextview

在我的项目中我使用这个项目:

DAKeyboardControlhttps://github.com/danielamitay/DAKeyboardControl

要在键盘上方附加UIToolbar UITextView并在使用平移手势解除键盘时移动它,现在我无法理解如何更改{{{ 1}}和UITextView在我编辑文字时添加UIToolbar我使用UITextView示例的相同代码,并使用{{更改DAKeyboardControl 1}}。

现在我如何动态更改文本视图高度?

3 个答案:

答案 0 :(得分:2)

将此代码放在viewDidLoad:

self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f,
                                                                     self.view.bounds.size.height-40,
                                                                     self.view.bounds.size.width,
                                                                     40.0f)];
    self.toolBar.barStyle = UIBarStyleBlack;

    self.toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:self.toolBar];

    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10.0f,6.0f,self.toolBar.bounds.size.width - 20.0f - 68.0f,30.0f)];
    self.textView.clipsToBounds = YES;
    self.textView.layer.cornerRadius = 5.0f;
    self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    [self.textView setKeyboardAppearance:UIKeyboardAppearanceAlert];
    [self.textView setFont:[UIFont tv_LektonRegular:16]];
    [self.textView setDelegate:self];

    [self.toolBar addSubview:self.textView];

    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    sendButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    [sendButton setTitle:NSLocalizedString(@"Send", @"") forState:UIControlStateNormal];
    [sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sendButton.titleLabel setFont:[UIFont tv_LektonRegular:18]];
    [sendButton addTarget:self action:@selector(sendComment:) forControlEvents:UIControlEventTouchUpInside];

    sendButton.frame = CGRectMake(self.toolBar.bounds.size.width - 68.0f,
                                  6.0f,
                                  58.0f,
                                  29.0f);
    [self.toolBar addSubview:sendButton];

    self.view.keyboardTriggerOffset = self.toolBar.bounds.size.height;

    __weak CommentActivityViewController* weakSelf = self;

    [self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView) {

        CGRect toolBarFrame = weakSelf.toolBar.frame;
        toolBarFrame.origin.y = keyboardFrameInView.origin.y - toolBarFrame.size.height;
        weakSelf.toolBar.frame = toolBarFrame;
    }];

然后:

- (void)textViewDidChange:(UITextView *)textView
{
    [self _updateInputViewFrameWithKeyboardFrame];
}

- (void)_updateInputViewFrameWithKeyboardFrame
{
    // Calculate the height the input view ideally
    // has based on its textview's content
    UITextView *textView = self.textView;

    CGFloat newInputViewHeight;
    if ([NSURLSession class])
    {
        newInputViewHeight = textViewHeight(textView);
    } else {
        newInputViewHeight = self.textView.contentSize.height;
    }

    //10 is the border of the uitoolbar top and bottom
    newInputViewHeight += 10;
    newInputViewHeight = ceilf(newInputViewHeight);
    //newInputViewHeight = MIN(maxInputViewHeight, newInputViewHeight);

    // If the new input view height equals the current,
    // nothing has to be changed
    if (self.textView.bounds.size.height == newInputViewHeight) {
        return;
    }
    // If the new input view height is bigger than the view available, do nothing
    if ((self.view.bounds.size.height - self.view.keyboardFrameInView.size.height < newInputViewHeight)) {
        return;
    }

    CGRect inputViewFrame = self.textView.frame;
    inputViewFrame.size.height = newInputViewHeight;
    self.textView.frame = inputViewFrame;

    CGRect toolBarFrame = self.toolBar.frame;
    toolBarFrame.size.height = newInputViewHeight +10;
    toolBarFrame.origin.y = self.view.keyboardFrameInView.origin.y - toolBarFrame.size.height;
    self.toolBar.frame = toolBarFrame;

    self.view.keyboardTriggerOffset = self.toolBar.bounds.size.height;
}

static inline CGFloat textViewHeight(UITextView *textView) {
    NSTextContainer *textContainer = textView.textContainer;
    CGRect textRect =
    [textView.layoutManager usedRectForTextContainer:textContainer];

    CGFloat textViewHeight = textRect.size.height +
    textView.textContainerInset.top + textView.textContainerInset.bottom;

    return textViewHeight;
}

此代码适用于iOS 7及更早版本...

答案 1 :(得分:0)

简单地:

CGRect textViewFrame = textView.frame;
textViewFrame.size.height = 200;// Or whatever you want, basically do your dynamic calculation before and assign the value here
textView.frame = textViewFrame;

如果您希望以动画方式调整大小,请将其嵌入到视图动画块中,如下所示:

   [UIView animateWithDuration:0.2 animations:^{
         CGRect textViewFrame = textView.frame;
         textViewFrame.size.height = 200;// Or whatever you want, basically do your dynamic calculation before and assign the value here
         textView.frame = textViewFrame;
 }];

答案 2 :(得分:0)

使用addKeyboardPanningWithActionHandler:actionHandler的{​​{1}}方法。在处理程序块中添加代码以在用户平移键盘时动态更改帧。您可能希望将DAKeyboardControl设置为在触摸到达keyboardTriggerOffset框架时允许平移。

这里的优点是,即使键盘没有通过平移显示/消失,也会相应地调整帧的大小,因为键盘框架中的所有更改都会调用此块。

示例代码:

textView

只是一个示例,您可能希望在指定高度的行中进行一些加法/减法,以正确调整[self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView) { CGRect textViewFrame = myTextView.frame; textViewFrame.size.height = keyboardFrameInView.origin.y; myTextView.frame = textViewFrame; }]; 的大小。