【问题标题】:Autoresizing UITextView IOS7自动调整 UITextView IOS7 的大小
【发布时间】:2014-01-18 20:58:23
【问题描述】:

我做了很多研究,但对我目前的情况没有任何帮助。我想要做的是自动调整大小UITextView,随着用户类型的增长而增长。它以默认高度开始,随着文本的增加自动增长。我使用interface builderUITextView 添加到我的UIView。现在我只需要帮助让它自动成长。我在 IOS7 中找到的答案说你使用[myTextView sizeToFit],这使它成为auto-resize,但看起来这只适用于添加programmaticallyUITextViews

【问题讨论】:

标签: ios ios7 uitextview sizetofit


【解决方案1】:

我为此创建了一个 UITextView 的子类:

https://github.com/MatejBalantic/MBAutoGrowingTextView

它是一个基于自动布局的轻量级 UITextView 子类,它会根据用户输入的大小自动增长和缩小,并且可以通过最大和最小高度来限制 - 所有这些都无需一行代码。

主要用于界面生成器,仅适用于自动布局。

【讨论】:

    【解决方案2】:

    我建议您在使用自定义解决方案之前尝试HPGrowingTextView

    如果你不喜欢它,你可以这样去做:

    • 创建一个带有初始帧的UITextView,并将其添加到UIView
    • 重写textViewDidChange: 方法并使用yourTextView.contentSize 属性获取内容的CGSize
    • 使用CGSizeheight 属性来设置UITextView 的高度,使用CGRectMake

    contentSize 为您提供 textView 中内容的确切大小,而无需使用 sizeWithFont:(已弃用)或 sizeWithAttributes:

    但问题是:如果您的 textView 包含在另一个 UIView 中,您可能需要根据需要将其设置为 autoresizingMasksUIViewAutoresizingFlexibleTopMarginUIViewAutoresizingFlexibleBottomMarginUIViewAutoresizingFlexibleHeight,以获得成功调整 textView 的超级视图的大小。

    试试看HPGrowingTextView的代码,你就会明白这个行为是如何实现的。

    【讨论】:

      【解决方案3】:

      您需要为myTextView 设置一个委托,并让它响应其文本中的更改。

      在您的视图控制器的界面中声明它符合UITextViewDelegate 协议,例如:

      @interface MyViewController : UIViewController <UITextViewDelegate>
      

      在您的视图控制器的-viewDidLoad 中添加:

      self.myTextView.delegate = self;
      

      然后实现-textViewDidChange:委托方法:

      - (void)textViewDidChange:(UITextView *)textView
      {
          if (textView != self.myTextView)
              return;
      
          CGFloat const horizontalPadding = 16.0f; // experiment with these padding values
          CGFloat const verticalPadding = 16.0f;   // until the textview resizes nicely
      
          CGSize maxSize = CGSizeMake(textView.bounds.size.width - horizontalPadding, CGFLOAT_MAX);
          CGSize textSize;
          if ([textView.text respondsToSelector:@selector(sizeWithAttributes:)]) {
              // iOS7 and above
              NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [NSValue valueWithCGSize:maxSize], NSViewSizeDocumentAttribute,
                                          textView.font, NSFontAttributeName, nil];
              textSize = [textView.text sizeWithAttributes:attributes];
          } else {
              // iOS6 and below
              textSize = [textView.text sizeWithFont:textView.font
                                   constrainedToSize:maxSize
                                       lineBreakMode:NSLineBreakByWordWrapping];
          }
      
          CGRect newFrame = textView.frame;
          newFrame.size.height = textSize.height + verticalPadding;
          textView.frame = newFrame;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-19
        • 2019-06-25
        • 1970-01-01
        • 2014-09-25
        • 1970-01-01
        • 2012-11-24
        相关资源
        最近更新 更多