gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:11:01 27 4
gpt4 key购买 nike

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

DAKeyboardControl : https://github.com/danielamitay/DAKeyboardControl

附加 UIToolbarUITextView在键盘上方并在使用平移手势关闭键盘时移动它,现在我不明白如何更改 UITextView 的高度和 UIToolbar当我编辑文本时,添加 UITextView我使用与 DAKeyboardControl 相同的代码示例,我更改 UITextFieldUITextView .

现在如何动态更改 TextView 高度?

最佳答案

将此代码放入 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 及更早版本...

关于ios - 使用 DAKeyboardControl 更改 uikeyboard 上方 uitextview 的动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22933586/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com