开发者

UITextview gains an extra line when it should not

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 tex开发者_如何学运维t 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜