0

如何根据文本视图增加工具栏的大小,单击返回时,它应该移动到新行。工具栏的长度应该随着文本在 uitextview 中输入而增加

 text view which is in toolbar 
my code is  below  

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 10, 200, 42)];
    textView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:textView];

    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320.f, 80.f)];

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(close)];
    toolbar.items = [NSArray arrayWithObjects:doneButton,barItem,nil];
    textView.inputAccessoryView=toolbar;
    textView.delegate=self;
    [self.view addSubview:toolbar];
}
4

3 回答 3

0

Add this code. It is not perfect and requires improvements but central idea is like that

- (void)textViewDidChange:(UITextView *)textView
{
    UIToolbar *toolbar = textView.superview; // you should use your stored property or instance variable here
    CGRect textViewFrame = textView.frame;
    textViewFrame.size.height = textView.contentSize.height;
    textView.frame = textViewFrame;

    textViewFrame.size.height += 40.0f; // the text view padding
    toolbar.frame = textViewFrame;
}
于 2013-10-22T12:58:59.220 回答
0

viewDidLoad中没有正确的视图帧/大小。您应该在viewDidLayoutSubviews中调整大小。在这里阅读更多:

如果您使用自动布局,请为您刚刚创建的每个视图分配足够的约束来控制视图的位置和大小。否则,实现 viewWillLayoutSubviews 和 viewDidLayoutSubviews 方法来调整视图层次结构中子视图的框架。请参阅“调整视图控制器的视图大小”。</p>

于 2013-10-22T13:10:59.680 回答
0

由于 textView 使用 ScrollView,因此检查 contentSize 似乎是一种很好、简单的方法来做你想做的事。
所以......为了简化storoj的例子:

- (void)textViewDidChange:(UITextView *)textView
{
    float newHeight = textView.contentSize.height;
    if(newHeight < 80){
    }
    else if (newHeight < self.view.frame.size.height){
        [toolbar setFrame:CGRectMake(0, 0, 320, newHeight)];
        [textView setFrame:CGRectMake(100, 10, 200, newHeight-40)];
    }
}

笔记:

  • UIToolbar *工具栏; (在您班级的 .h 文件中声明)
  • 这些是特定于您的示例的硬编码值,因此请根据您的要求对其进行优化
  • (newHeight < 80) -- 其中 80 是工具栏的初始高度或最小高度
  • (newHeight < self.view.frame.size.height) -- self.view.frame.size.height 为您提供整个视图的高度,并防止增加子视图框架超出视图边界。

不完美,但从这里开始。

于 2013-10-22T13:32:52.700 回答