Iphone 多行文本字段(类似于SMS)和/或';完成';UITextView中的按钮
我已经研究了几天了,希望能得到一些帮助。有没有办法在SMS应用程序中生成类似Apple的多行UITextField?该控件的有用之处在于,它具有“凹陷”外观,清楚地表明它是一个文本输入框,但同时,它在每一个新行字符上展开 如果不能做到这一点,如果我被迫使用UITextView,有人能建议如何最好地关闭键盘吗?“完成”和“开始”按钮都只是用来生成换行符(“\n”)。对我来说,这似乎是错误的-当然至少其中一个应该生成不同的字符,这样我仍然可以允许换行符,但也可以在特定的按键上关闭键盘 我是不是错过了一些简单的东西Iphone 多行文本字段(类似于SMS)和/或';完成';UITextView中的按钮,iphone,uitextfield,uitextview,Iphone,Uitextfield,Uitextview,我已经研究了几天了,希望能得到一些帮助。有没有办法在SMS应用程序中生成类似Apple的多行UITextField?该控件的有用之处在于,它具有“凹陷”外观,清楚地表明它是一个文本输入框,但同时,它在每一个新行字符上展开 如果不能做到这一点,如果我被迫使用UITextView,有人能建议如何最好地关闭键盘吗?“完成”和“开始”按钮都只是用来生成换行符(“\n”)。对我来说,这似乎是错误的-当然至少其中一个应该生成不同的字符,这样我仍然可以允许换行符,但也可以在特定的按键上关闭键盘 我是不是错过了
提前感谢:)Facebook发布了一个名为的开源软件包,它有一个多行文本字段。您可以很容易地将其用于扩展文本字段 至于“完成”按钮,您可以将视图控制器设置为
UITextFieldDelegate
。然后使用此方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Do whatever you want for your done button
return YES;
}
如果是三个20,请使用以下方法TTTextEditorDelegate
:
- (BOOL)textFieldShouldReturn:(TTTextEditor *)textField {
// Do whatever you want for your done button
return YES;
}
我也遇到了类似的问题,我最终使用的是创建一个禁用的UITextField作为背景,并在其上方创建一个UITextView来获取输入。。。iPhone API在默认情况下不能有这样的功能,这真是太糟糕了。还要注意,这不会自动展开,但如果需要,可以通过处理
textViewDidChange:
至于处理返回键,请尝试从UITextViewDelegate
实现以下方法:
- (void)textViewDidChange:(UITextView *)inTextView {
NSString *text = inTextView.text;
if ([text length] > 0 && [text characterAtIndex:[text length] -1] == '\n') {
inTextView.text = [text substringToIndex:[text length] -1]; // remove last return from text view
[inTextView resignFirstResponder]; // hide keyboard
}
}
- (无效)TextEditordBeginEditing:(TTTextEditor*)textEditor{
及
- (void)TextEditorDediting:(TTTextEditor*)textEditor{ 可能就是你想要的。享受吧
// Input
_inputBackgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, size.height - _InputBarHeight, size.width, _InputBarHeight)];
_inputBackgroundView.autoresizingMask = UIViewAutoresizingNone;
_inputBackgroundView.contentMode = UIViewContentModeScaleToFill;
_inputBackgroundView.userInteractionEnabled = YES;
[self addSubview:_inputBackgroundView];
[_inputBackgroundView release];
[_inputBackgroundView setImage:[[UIImage imageNamed:@"Footer_BG.png"] stretchableImageWithLeftCapWidth:80 topCapHeight:25]];
// Text field
_textField = [[UITextView alloc] initWithFrame:CGRectMake(70.0f, 0, 185, 0)];
_textField.backgroundColor = [UIColor clearColor];
_textField.delegate = self;
_textField.contentInset = UIEdgeInsetsMake(-4, -2, -4, 0);
_textField.showsVerticalScrollIndicator = NO;
_textField.showsHorizontalScrollIndicator = NO;
_textField.font = [UIFont systemFontOfSize:15.0f];
[_inputBackgroundView addSubview:_textField];
[_textField release];
[self adjustTextInputHeightForText:@""];
填写UITextView委托方法:
- (void) textViewDidBeginEditing:(UITextView*)textView {
[self adjustTextInputHeightForText:_textField.text];
}
- (void) textViewDidEndEditing:(UITextView*)textView {
[self adjustTextInputHeightForText:_textField.text];
}
- (BOOL) textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
if ([text isEqualToString:@"\n"])
{
[self performSelector:@selector(inputComplete:) withObject:nil afterDelay:.1];
return NO;
}
else if (text.length > 0)
{
[self adjustTextInputHeightForText:[NSString stringWithFormat:@"%@%@", _textField.text, text]];
}
return YES;
}
- (void) textViewDidChange:(UITextView*)textView {
[self adjustTextInputHeightForText:_textField.text];
}
诀窍是
- (void) adjustTextInputHeightForText:(NSString*)text {
int h1 = [text sizeWithFont:_textField.font].height;
int h2 = [text sizeWithFont:_textField.font constrainedToSize:CGSizeMake(_textField.frame.size.width - 16, 170.0f) lineBreakMode:UILineBreakModeWordWrap].height;
[UIView animateWithDuration:.1f animations:^
{
if (h2 == h1)
{
_inputBackgroundView.frame = CGRectMake(0.0f, self.frame.size.height - _InputBarHeight, self.frame.size.width, _InputBarHeight);
}
else
{
CGSize size = CGSizeMake(_textField.frame.size.width, h2 + 24);
_inputBackgroundView.frame = CGRectMake(0.0f, self.frame.size.height - size.height, self.frame.size.width, size.height);
}
CGRect r = _textField.frame;
r.origin.y = 12;
r.size.height = _inputBackgroundView.frame.size.height - 18;
_textField.frame = r;
} completion:^(BOOL finished)
{
//
}];
}
您好……UITextView没有textViewShouldReturn方法,即使设置为UITextViewDelegate,也不起作用。我也不喜欢使用Three20的东西,因为我已经阅读了关于Three20使用的私有API导致多个应用程序从应用商店被拒绝的报告。私有API引用早就被拒绝了从Three20中删除(请参阅2009年11月17日的更改列表)。正如去年12月在iPhone Tech Talk World Tour上向我解释的那样,您最终未使用的任何代码都会从发布二进制文件中剥离。请参阅Xcode构建系统指南(iPhone OS参考库的一部分)中的“死代码剥离”。感谢您对UITextViewDelegate的建议,但恐怕这不会起作用。因为我想允许多行输入,所以无法扫描“\n”-这就是为什么我觉得“Go”和“Done”这两种生成行为都没有帮助的原因。好的,建议的解决方案(我也在使用)是添加一个“Done”按钮指向
UINavigationBar
,作为UINavigationItem
的rightbuttonItem
,单击它时,只需在文本视图上单击resignFirstResponder
。或者,基本上,只需添加UI的任何其他元素来退出响应程序。不过,导航栏似乎被广泛使用。嗨,现在你的代码没有ompile(例如,_InputBarHeight、Footer_BG.png图像等)。如果您可以用代码创建一个最小的项目,并在此处传递一个链接,让所有需要它的人都可以下载(可能是github?)我相信很多人,包括我在内,都会非常感激:)不客气:)我用这个样本@MaratAl创建了git回购,非常感谢!