我想知道是否有人可以解释我的应用程序中发生的事情。我使用UITextView创建了一个多行文本框。当视图加载时,它是一个单行文本框,当用户输出他们的消息时,我会增加大小。几乎是如何使用SMS应用程序。
问题是在添加新行时它正在做一些相当时髦的事情。当你想要添加一条新线时,当你想要添加2条新线时,当你输入另一条线时,第二条额外线就会消失,而我只留下1条新线线。
在添加每个字母时,我已将我的值输出到控制台。所有值都是正确的,即使添加第二个附加行,在数学上和代码方面,textview的高度值是相同的,有和没有这个第二行1个字符。 (使用调试器检查各种值时会发生相同的结果)
我已将我的功能放在下面,为你编码boffins以引起你的注意并告诉我我做错了什么。
-(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];
}
如果有人可以帮助请,请不要留下太多头发。非常感谢提前。