我已经研究了几天了,并且会感激一点帮助。有没有办法在SMS应用程序中生成像Apple一样的多行UITextField?关于这个控件的有用之处在于它具有“沉没”外观,可以清楚地表明它是一个文本输入框,但同时它会扩展每个换行符。
如果不这样做,如果我被迫使用UITextView,任何人都可以建议如何最好地解雇键盘? “完成”和“开始”按钮似乎都会生成换行符('\ n')。这对我来说似乎是错的 - 当然至少其中一个应该生成一个不同的角色,这样我仍然可以允许换行符,但也可以在特定按键时关闭我的键盘。
我在这里错过了一些简单的东西吗?
提前致谢:)
答案 0 :(得分:9)
也许你可以建立我写的课程?这与tttexteditor相同,没有丑陋的故障:http://www.hanspinckaers.com/multi-line-uitextview-similar-to-sms
答案 1 :(得分:3)
一个老问题,但经过几个小时后,我已经想出如何使它完全像在Instagram中一样(它在所有BTW中具有最佳算法)
初始化:
// 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)
{
//
}];
}
答案 2 :(得分:2)
Facebook发布了一个名为Three20的开源软件包,它有一个多行文本字段。您可以非常轻松地将其用于扩展文本字段。
对于“完成”按钮,您可以将视图控制器设置为UITextFieldDelegate
。然后使用这个方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Do whatever you want for your done button
return YES;
}
对于Three20,请使用TTTextEditorDelegate
:
- (BOOL)textFieldShouldReturn:(TTTextEditor *)textField {
// Do whatever you want for your done button
return YES;
}
答案 3 :(得分:1)
好吧,我遇到了类似的问题,我最终使用的是实际创建一个禁用的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
}
}
答案 4 :(得分:0)
(void)textEditorDidBeginEditing:(TTTextEditor *)textEditor { 和