目标C:如何创build一个多行的UITextField?

可能重复:
如何创build一个多行UITextfield?

我如何实现一个多行文本字段,就像我在iPhone消息应用程序中看到的一样?

在这里输入图像说明

看起来好像一旦input的长度超过了长度,第二行就会自动创build。

编辑:更新,以澄清我使用UITextView的挑战

对于我来说,我想将UITextView的感觉和外观build模为如下所示。 我不知道如何才能用UITextView做到这一点。 例如

1)如果我的文本需要溢出到下一行,如何dynamic扩展框

2)如何添加边框和样式如下所示

3)如何将文本框的正上方的键盘(而不是在视图中)

我知道Instagram也有这个function,如果有人能指出我正确的方向,那将会很棒。 谢谢你

在这里输入图像说明

检查这些答案:

如何创build一个多行UITextfield?

如何创build一个多行UITextfield?

http://brettschumann.com/blog/2010/01/15/iphone-multiline-textbox-for-sms-style-chat

并试图尝试Three20这是一个伟大的图书馆,在许多应用程序,如Facebook使用。


编辑:添加从BrettSchumann博客提取

 #import <uikit uikit.h=""> @interface MultilineTextboxViewController : UIViewController { IBOutlet UIView *viewTable; IBOutlet UIView *viewForm; IBOutlet UITextView *chatBox; IBOutlet UIButton *chatButton; } @property (nonatomic, retain) UIView *viewTable; @property (nonatomic, retain) UIView *viewForm; @property (nonatomic, retain) UITextView *chatBox; @property (nonatomic, retain) UIButton *chatButton; - (IBAction)chatButtonClick:(id)sender; @end </uikit> 

完成之后,我们将所有东西都放在一起,让我们将项目添加到我们的主(.m)文件中,同时不要忘记取消分配它们。

 @synthesize viewTable; @synthesize viewForm; @synthesize chatBox; @synthesize chatButton; - (void)dealloc{ [viewTable release]; [viewForm release]; [chatBox release]; [chatButton release]; [super dealloc]; } 

在(void)viewDidLoad方法中,我们需要添加一些通知观察器,以便我们可以看到键盘显示或隐藏的时间以及用户何时按下键盘上的按键。

 - (void)viewDidLoad { //set notification for when keyboard shows/hides [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; //set notification for when a key is pressed. [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(keyPressed:) name: UITextViewTextDidChangeNotification object: nil]; //turn off scrolling and set the font details. chatBox.scrollEnabled = NO; chatBox.font = [UIFont fontWithName:@"Helvetica" size:14]; [super viewDidLoad]; } 

当在textview上设置焦点时,键盘将显示。 由于textview和button都位于屏幕的下方viewForm中,因此键盘将骑上去并重新进行操作。 所以我们要做的就是调整viewTable的高度和viewForm的位置。 我们用下面的方法做这个。

 -(void) keyboardWillShow:(NSNotification *)note{ // get keyboard size and loction CGRect keyboardBounds; [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds]; // get the height since this is the main value that we need. NSInteger kbSizeH = keyboardBounds.size.height; // get a rect for the table/main frame CGRect tableFrame = viewTable.frame; tableFrame.size.height -= kbSizeH; // get a rect for the form frame CGRect formFrame = viewForm.frame; formFrame.origin.y -= kbSizeH; // animations settings [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.3f]; // set views with new info viewTable.frame = tableFrame; viewForm.frame = formFrame; // commit animations [UIView commitAnimations]; } 

现在,键盘显示,我们的意见已经调整,我们要捕捉用户正在进入的文本,看看我们是否需要对chatBox进行任何调整。 幸运的是,我们可以使用CGSize对象,传递一些文本,并告诉对象我们想要约束文本的大小,我们可以计算得到高度。现在这是一个小小的试验和错误的地方。该行随字体的大小而变化,并且您的CGSize对象的宽度将随着您的UITextview的宽度而变化,因此您将不得不做一点实验。 在下面的代码中使用12的值是我chatBox的起始高度和基于我设置的字体的行高度之间的高度差异。

 -(void) keyPressed: (NSNotification*) notification{ // get the size of the text block so we can work our magic CGSize newSize = [chatBox.text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] constrainedToSize:CGSizeMake(222,9999) lineBreakMode:UILineBreakModeWordWrap]; NSInteger newSizeH = newSize.height; NSInteger newSizeW = newSize.width; // I output the new dimensions to the console // so we can see what is happening NSLog(@"NEW SIZE : %d X %d", newSizeW, newSizeH); if (chatBox.hasText) { // if the height of our new chatbox is // below 90 we can set the height if (newSizeH <= 90) { [chatBox scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO]; // chatbox CGRect chatBoxFrame = chatBox.frame; NSInteger chatBoxH = chatBoxFrame.size.height; NSInteger chatBoxW = chatBoxFrame.size.width; NSLog(@"CHAT BOX SIZE : %d X %d", chatBoxW, chatBoxH); chatBoxFrame.size.height = newSizeH + 12; chatBox.frame = chatBoxFrame; // form view CGRect formFrame = viewForm.frame; NSInteger viewFormH = formFrame.size.height; NSLog(@"FORM VIEW HEIGHT : %d", viewFormH); formFrame.size.height = 30 + newSizeH; formFrame.origin.y = 199 - (newSizeH - 18); viewForm.frame = formFrame; // table view CGRect tableFrame = viewTable.frame; NSInteger viewTableH = tableFrame.size.height; NSLog(@"TABLE VIEW HEIGHT : %d", viewTableH); tableFrame.size.height = 199 - (newSizeH - 18); viewTable.frame = tableFrame; } // if our new height is greater than 90 // sets not set the height or move things // around and enable scrolling if (newSizeH > 90) { chatBox.scrollEnabled = YES; } } } 

一旦我们和用户按下发送button,我们想要用我们的文本做一些事情,关键字将会消失,我们希望重新恢复我们的观点。 那么我们该怎么做呢?

 - (IBAction)chatButtonClick:(id)sender{ // hide the keyboard, we are done with it. [chatBox resignFirstResponder]; chatBox.text = nil; // chatbox CGRect chatBoxFrame = chatBox.frame; chatBoxFrame.size.height = 30; chatBox.frame = chatBoxFrame; // form view CGRect formFrame = viewForm.frame; formFrame.size.height = 45; formFrame.origin.y = 415; viewForm.frame = formFrame; // table view CGRect tableFrame = viewTable.frame; tableFrame.size.height = 415; viewTable.frame = tableFrame; } 

resignFirstResponder将隐藏键盘,然后我们所要做的就是将视图和chatBox设置回原来的状态。

 -(void) keyboardWillHide:(NSNotification *)note{ // get keyboard size and loction CGRect keyboardBounds; [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds]; // get the height since this is the main value that we need. NSInteger kbSizeH = keyboardBounds.size.height; // get a rect for the table/main frame CGRect tableFrame = viewTable.frame; tableFrame.size.height += kbSizeH; // get a rect for the form frame CGRect formFrame = viewForm.frame; formFrame.origin.y += kbSizeH; // animations settings [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.3f]; // set views with new info viewTable.frame = tableFrame; viewForm.frame = formFrame; // commit animations [UIView commitAnimations]; } 

在那里你可以看到,一个以单行开始的文本视图,随着用户在成为滚动文本视图之前将文本input到最大大小,文本视图的大小会增加。

UITextField不允许你写多行…但是你可以使用UITextView来代替,并使用NSString -sizeWithFont函数来理解你需要多less空间。

喜欢这个:

 CGSize size = [yourTextView.text sizeWithFont:yourTextView.font]; CGRect f = yourTextView.frame; f.size.height = ceil(size.width/f.size.width)*size.height yourTextView.frame = f; 

我没有试过这个代码,但是我已经使用了过去的代码。 我希望我的信息可能是有用的:)