UITextview 在不应该的情况下获得了额外的一行

发布于 2024-08-15 03:03:37 字数 2731 浏览 0 评论 0原文

我想知道是否有人可以阐明我的应用程序中发生的情况。我使用 UITextView 创建了一个多行文本框。当视图加载时,它是一个单行文本框,当用户输入他们的消息时,我会相应地增加大小。短信应用程序的工作原理差不多。

问题是它在添加新行时做了一些相当时髦的事情。发生的情况是,当您到达行尾时,当要添加新行时,它似乎添加了 2 个新行,一旦您输入另一个字符,第二个附加行就会消失,我只剩下 1 个新的附加行线。

添加每个字母时,我已将我的值输出到控制台。所有值都是正确的,即使添加了第二行,从数学和代码角度来看,文本视图的高度值在有和没有这第二行 1 个字符的情况下都是相同的。 (使用调试器检查各种值时会发生相同的结果)

我将我的函数放在下面,供代码研究人员注意并告诉我我做错了什么。

-(void) keyPressed: (NSNotification*) notification{
    // check if there is text in the text box
    if (chatTextView.hasText)
    {
        chatTextButton.enabled = YES;
        CGSize expectedSize = [[[notification object]text] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(210,9999) lineBreakMode:UILineBreakModeWordWrap];
        NSInteger expectedHeight = expectedSize.height;

        if (expectedHeight < 30)
        {
            [chatTextView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
        }

        
           if (expectedHeight >= 30 && expectedHeight <= 126)
        {

            //text view resizing
            CGRect frameTextView = chatTextView.frame;
            NSInteger frameTextW = frameTextView.size.width;
            frameTextView.size.height = expectedHeight + 12;
            chatTextView.frame = frameTextView;

            //chat view resizing
            CGRect frameChat = chatTextBoxView.frame;
            NSInteger frameChatH = frameChat.size.height;
            NSInteger frameChatY = frameChat.origin.y;
            frameChat.origin.y = 202 - (expectedHeight + 27);
            frameChat.size.height = (expectedHeight + 12);
            chatTextBoxView.frame = frameChat;

            //main view resizing
            CGRect frameMain = self.view.frame;
            NSInteger frameMainH = frameMain.size.height;
            frameMain.size.height = 247 - (expectedHeight + 27);
            self.view.frame = frameMain;

            NSLog(@"==== EXPECTED  HEIGHT %d =====",expectedHeight);
            NSLog(@"==== CHAT TEXT WIDTH  %d =====",frameTextW);
            NSLog(@"==== CHAT VIEW HEIGHT %d =====",frameChatH);
            NSLog(@"==== CHAT VIEW LOCATY %d =====",frameChatY);
            NSLog(@"==== MAIN VIEW HEIGHT %d =====",frameMainH);
            NSLog(@"==============================");

            [chatTextView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
        }
        if (expectedHeight > 126)
        {
            chatTextView.scrollEnabled = YES;
        }
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3f];
    [UIView commitAnimations];

    [self scrollThread];
}

如果有人能帮忙请拜托,我已经没有多少头发了。非常感谢。

I wonder if there is someone out there that can shed some light as to what is happening in my app. I have created a multiline text box using the UITextView. When the view loads it is a single line textbox and as the user types out their message I increase the size according. Pretty much how the SMS app works.

The problem is it is doing something rather funky when adding new lines. What happens is when you get to end of the line when it is meant to add a new line it seems to add 2 new lines and as soon as you enter another character the 2nd additional line disappears and I am left with just the 1 new additional line.

I have outputted my values to the console when as each letter is added. All the values are correct, even when the 2nd additional line is added, mathematically and code wise the height value of the textview is the same with and without the this 2nd line for 1 character. (The same results happen when using the debugger to inspect the various values)

I have placed my function below for you code boffins to cast your eye over and tell me what I am doing wrong.

-(void) keyPressed: (NSNotification*) notification{
    // check if there is text in the text box
    if (chatTextView.hasText)
    {
        chatTextButton.enabled = YES;
        CGSize expectedSize = [[[notification object]text] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(210,9999) lineBreakMode:UILineBreakModeWordWrap];
        NSInteger expectedHeight = expectedSize.height;

        if (expectedHeight < 30)
        {
            [chatTextView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
        }

        
           if (expectedHeight >= 30 && expectedHeight <= 126)
        {

            //text view resizing
            CGRect frameTextView = chatTextView.frame;
            NSInteger frameTextW = frameTextView.size.width;
            frameTextView.size.height = expectedHeight + 12;
            chatTextView.frame = frameTextView;

            //chat view resizing
            CGRect frameChat = chatTextBoxView.frame;
            NSInteger frameChatH = frameChat.size.height;
            NSInteger frameChatY = frameChat.origin.y;
            frameChat.origin.y = 202 - (expectedHeight + 27);
            frameChat.size.height = (expectedHeight + 12);
            chatTextBoxView.frame = frameChat;

            //main view resizing
            CGRect frameMain = self.view.frame;
            NSInteger frameMainH = frameMain.size.height;
            frameMain.size.height = 247 - (expectedHeight + 27);
            self.view.frame = frameMain;

            NSLog(@"==== EXPECTED  HEIGHT %d =====",expectedHeight);
            NSLog(@"==== CHAT TEXT WIDTH  %d =====",frameTextW);
            NSLog(@"==== CHAT VIEW HEIGHT %d =====",frameChatH);
            NSLog(@"==== CHAT VIEW LOCATY %d =====",frameChatY);
            NSLog(@"==== MAIN VIEW HEIGHT %d =====",frameMainH);
            NSLog(@"==============================");

            [chatTextView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
        }
        if (expectedHeight > 126)
        {
            chatTextView.scrollEnabled = YES;
        }
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3f];
    [UIView commitAnimations];

    [self scrollThread];
}

If anyone can help please please, I don' have much hair left. Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文