1

我可以使用下面的代码来设置 textView 框架的大小,以在我按下按钮或诸如此类时大致匹配 textView 的内容(键入的文本)。每当输入一个新字符时,我将如何调用它,以便框架会交互地增长或缩小?

- (IBAction)doneEditingText:(id)sender {
    [myTextView resignFirstResponder];
    [myTextView setFrame:CGRectMake(myTextView.frame.origin.x, myTextView.frame.origin.y, myTextView.contentSize.width, myTextView.contentSize.height)];
}

谢谢阅读

4

3 回答 3

4

编辑

在 .h 文件中实现 UITextView 委托:

@interface ViewController : UIViewController<UITextViewDelegate>

如果 yourTextView 从 xib 添加,则将委托与文件所有者绑定,否则在 ViewDidLoad 添加此行:

yourTextView.delegate = self;

根据您的要求使用 textView 的委托:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
   CGSize maximumSize = CGSizeMake(280,999); //specify width of textView  and maximum height for text to fit in width of textView
   CGSize txtSize = [textView.text sizeWithFont:[UIFont fontWithName:@"Arial" size:16] constrainedToSize:maximumSize lineBreakMode:UILineBreakModeCharacterWrap]; //calulate size of text by specifying font here
   //Add UIViewAnimation here if needed
   [textView setFrame:CGRectMake(textView.frame.origin.x,textView.frame.origin.y,txtSize.width+10,txtSize.height+10)]; // change accordingly
   return YES;
}
于 2012-08-31T10:29:41.853 回答
3

我刚刚完成了这个。当前(发布此答案时)接受的答案的问题是委托方法:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

在提交用户键入/插入/删除的更改之前公开 textView。因此,您将要实现的调整大小将晚一个字符。UITextView 确实从 UIScrollView 继承,因此文本不会从屏幕上剪掉,但可能会导致一些尴尬的行为。

我的解决方案是使用两个委托方法来正确实现调整大小的效果。

在用户键入的字符到达屏幕之前展开 UITextView:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSMutableString *tempString = [NSMutableString stringWithString:textView.text];

[tempString replaceCharactersInRange:range withString:text];

//If we are adding to the length of the string (We might need to expand)
if([tempString length]>textView.text.length)
{
    //Create a temporaryTextView which has all of the characteristics of your original textView
    UITextView *tempTextView = [[UITextView alloc] initWithFrame:CGRectZero];
    tempTextView.font = _inputFont;
    tempTextView.contentInset = textView.contentInset;
    [tempTextView setText:tempString];

    //Change this to respect whatever width constraint you are trying to achieve.
    CGSize theSize = [tempTextView sizeThatFits:CGSizeMake(192, CGFLOAT_MAX)];

    if(theSize.height!=textView.frame.size.height)
    {

        textView.frame = CGRectMake(115, 310, 192,theSize.height);

        return YES;
    }
    else
    {
        return YES;
    }

}
else
{
    return YES;
}
}

并在用户删除/缩小 UITextView 中的文本量后缩小字符

-(void)textViewDidChange:(UITextView *)textView
{
//We enter this method AFTER the edit has been drawn to the screen, therefore check to see if we should shrink.
if([textView sizeThatFits:CGSizeMake(192, CGFLOAT_MAX)].height!=textView.frame.size.height)
{
    //change this to reflect the constraints of your UITextView
    textView.frame = CGRectMake(115, 310, 192,[textView sizeThatFits:CGSizeMake(192, CGFLOAT_MAX)].height);


}
}
于 2013-01-31T15:06:02.200 回答
3

您可以在 GIT 上使用类似这个 repo 的东西,它具有您想要的几乎相同的功能-

https://github.com/HansPinckaers/GrowingTextView 它类似于 iPhone 中的消息应用程序。

于 2012-08-31T10:24:47.153 回答