gpt4 book ai didi

ios - 当 UITextView 只有单行时,如何调整 UITextView 的大小

转载 作者:行者123 更新时间:2023-11-29 00:31:17 29 4
gpt4 key购买 nike

我用 View 制作了一个 UITextview 自定义控件。它工作正常。当我输入新行时, TextView 会随着 View (深蓝色)而增加。此后 TextViell 将以滚动的形式工作。

代码是

-(void)setTextViewHeight
{
self.oldFrameHeight = self.commentTextView.frame.size.height;
CGFloat fixedWidth = self.commentTextView.frame.size.width;
CGSize newSize = [self.commentTextView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = self.commentTextView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
self.commentTextView.frame = newFrame;
self.newFrameHeight = newFrame.size.height;
NSLog(@"Old Frame %f",self.oldFrameHeight);
NSLog(@"New Frame %f",self.newFrameHeight);
}
- (void)textViewDidChange:(UITextView *)textView
{


self.oldFrameHeight = textView.frame.size.height;


if(!(self.oldFrameHeight >= 44))
{
[self setTextViewHeight];

if (self.newFrameHeight > self.oldFrameHeight)
{
[self sabViewFrameChange];
}
}
else if ([textView.text isEqualToString:@""])
{
[self.commentTextView setFrame:CGRectMake(60, 10,self.frame.size.width - 130, 30)];

UIViewController *controller=(UIViewController *)self.delegate;
self.frame = CGRectMake(0,controller.view.frame.size.height - 50, controller.view.frame.size.width,50);
self.height = 50;
}
else
{
self.commentTextView.scrollEnabled = YES;
}
}

我的问题是

当我从 UITextView 中删除第二行时,大小将保持原样,我没有随行减小。

请帮帮我。提前致谢。

图片是

1) 多于两行

enter image description here

2) 单行

enter image description here

我该如何解决这个问题?有人有任何想法吗?

最佳答案

声明numLinesInTextView变量

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
NSLog(@"%@",text);
if ([text isEqualToString:@"\n"]) {
NSLog(@"New line");
}else{
int numLines = [textView intrinsicContentSize].height / textView.font.lineHeight;
NSLog(@"%f",textView.font.lineHeight);

numLinesInTextView = numLines;
if (numLinesInTextView == 0) {
[self.commentTextView setFrame:CGRectMake(60, 10,self.frame.size.width - 130, 30)];
UIViewController *controller=(UIViewController *)self.delegate;
self.frame = CGRectMake(0,controller.view.frame.size.height - 50, controller.view.frame.size.width,50);
height = 50;
numLinesInTextView = 0;
}

}
return YES;
}

将此方法添加到您的项目中并检查其工作是否完全正常。现在。

快乐编码。

关于ios - 当 UITextView 只有单行时,如何调整 UITextView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41712208/

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