27

UITextViewUIView. 添加的textview是不可编辑的,它只是为了显示一些数据。textview 中显示的数据是动态的。那就是行数不固定。它可能会有所不同。所以如果行数增加,textview的大小也需要增加。我不知道该怎么做。请给我一些想法。

更新:

这就是我正在做的事情:

UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
baseView.backgroundColor = [UIColor grayColor];
[window addSubview:baseView];

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 100, 30)];
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
textView.text = @"asdf askjalskjalksjlakjslkasj";
[textView sizeToFit];
[baseView addSubview:textView];
4

10 回答 10

58

How do I size a UITextView to its content? 上发布了一个答案?

CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;

或更好(考虑到 contentInset 感谢 kpower 的评论)

CGRect frame = _textView.frame;
UIEdgeInsets inset = textView.contentInset;
frame.size.height = _textView.contentSize.height + inset.top + inset.bottom;
_textView.frame = frame;

注意:如果您要多次引用对象的属性(例如 frame 或 contentInset),最好将其分配给局部变量,这样您就不会触发额外的方法调用(_textView.frame/[_textView frame] are method来电)。如果您多次调用此代码(100000 次),那么这将明显变慢(十几个方法调用是微不足道的)。

但是...如果您想在没有额外变量的情况下在一行中执行此操作,那将是

_textView.frame = CGRectMake(_textView.frame.origin.x, _textView.frame.origin.y, _textView.frame.size.width, _textView.contentSize.height + _textView.contentInset.top + _textView.contentInset.bottom);

以 5 次额外的方法调用为代价。

于 2010-05-17T16:54:48.897 回答
30

您可以使用setFrame:sizeToFit

更新:

我使用sizeToFitwith UILabel,它工作得很好,但UITextView它是 的子类UIScrollView,所以我可以理解为什么sizeToFit不能产生预期的结果。

您仍然可以计算文本高度并使用,但如果文本太长setFrame,您可能想利用's 滚动条。UITextView

以下是获取文本高度的方法:

#define MAX_HEIGHT 2000

NSString *foo = @"Lorem ipsum dolor sit amet.";
CGSize size = [foo sizeWithFont:[UIFont systemFontOfSize:14]
              constrainedToSize:CGSizeMake(100, MAX_HEIGHT)
                  lineBreakMode:UILineBreakModeWordWrap];

然后你可以用你的UITextView

[textView setFont:[UIFont systemFontOfSize:14]];
[textView setFrame:CGRectMake(5, 30, 100, size.height + 10)];

或者您可以先进行高度计算并避开这setFrame条线:

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 100, size.height + 10)];
于 2009-04-08T09:33:45.927 回答
4

sizeToFit 确实有效

如果在第一次调整文本大小后调用 sizeToFit。因此,在您第一次设置它之后,对设置文本的后续调用将不会导致大小发生变化。即使您调用 sizeToFit。

但是,您可以强制它像这样调整大小:

  1. 设置文本。
  2. 将 textView 框架高度更改为 CGFLOAT_MAX。
  3. 调用 sizeToFit。
于 2013-08-07T19:16:38.440 回答
3

textView.contentSize.height在文本实际增长textViewDidChange只能调整大小。为了获得最佳视觉效果,最好事先调整大小。几个小时后,我想出了如何让它和 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)
   {
      //
   }];
}
于 2012-11-04T18:21:11.227 回答
2

这对我来说非常有效:

#define MAX_HEIGHT 2000     
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14]
      constrainedToSize:CGSizeMake(100, MAX_HEIGHT)
          lineBreakMode:UILineBreakModeWordWrap];

[textview setFont:[UIFont systemFontOfSize:14]];
[textview setFrame:CGRectMake(45, 6, 100, size.height + 10)];
textview.text = text;
于 2011-07-19T20:45:50.763 回答
1

请执行下列操作:

_textView.text = someText;
[_textView sizeToFit];
_textView.frame.height = _textView.contentSize.height;
于 2013-09-30T02:26:50.277 回答
1

为了解决类似的问题,我刚刚创建了一个基于自动布局的轻量级 UITextView 子类,它会根据用户输入的大小自动增长和缩小,并且可以受最大和最小高度的约束——所有这些都无需一行代码。

https://github.com/MatejBalantic/MBAutoGrowingTextView

于 2014-05-16T13:19:29.490 回答
1

@Gabe 给出的答案似乎直到 viewDidAppear 之后才在 iOS7.1 中起作用。请参阅下面的测试。

更新:实际上,情况更加复杂。如果您在 resizeTheTextView 方法中分配 textView.text,则在 iOS7 中,调整大小相当于只允许单行文本。严重奇怪。

UPDATE2:另请参阅iOS7 中不同的 UITextView 内容大小

UPDATE3:请参阅最底部的代码了解我现在正在使用的内容。似乎完成了这项工作。

#import "ViewController.h"

@interface ViewController ()
{
    UITextView *textView;
}
@end

@implementation ViewController

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

    textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 200, 1)];
    [self.view addSubview:textView];
    CALayer *layer = textView.layer;
    layer.borderColor = [UIColor blackColor].CGColor;
    layer.borderWidth = 1;

    textView.text = @"hello world\n\n";

    // Calling the method directly, after the view is rendered, i.e., after viewDidAppear, works on both iOS6.1 and iOS7.1
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"Change size" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(resizeTheTextView) forControlEvents:UIControlEventTouchUpInside];
    [button sizeToFit];
    CGRect frame = button.frame;
    frame.origin.y = 400;
    button.frame = frame;
    [self.view addSubview:button];

    // Works on iOS6.1, but does not work on iOS7.1
    //[self resizeTheTextView];
}

- (void) viewWillAppear:(BOOL)animated
{
    // Does not work on iOS7.1, but does work on iOS6.1
    //[self resizeTheTextView];
}

- (void) viewDidAppear:(BOOL)animated
{
    // Does work on iOS6.1 and iOS7.1
    //[self resizeTheTextView];
}

- (void) resizeTheTextView
{
    NSLog(@"textView.frame.size.height: %f", textView.frame.size.height);

    NSLog(@"textView.contentSize.height: %f", textView.contentSize.height);

    // 5) From https://stackoverflow.com/questions/728704/resizing-uitextview
    CGRect frame = textView.frame;
    UIEdgeInsets inset = textView.contentInset;
    frame.size.height = textView.contentSize.height + inset.top + inset.bottom;
    textView.frame = frame;

    NSLog(@"inset.top: %f, inset.bottom: %f",  inset.top,  inset.bottom);

    NSLog(@"textView.frame.size.height: %f", textView.frame.size.height);

    NSLog(@"textView.contentSize.height: %f", textView.contentSize.height);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

更新3:

if ([[UIDevice currentDevice] majorVersionNumber] < 7.0) {
    CGRect frame = _abstractTextView.frame;
    UIEdgeInsets inset = _abstractTextView.contentInset;
    frame.size.height = _abstractTextView.contentSize.height + inset.top + inset.bottom;
    _abstractTextView.frame = frame;
}
else {
    CGSize textViewSize = [_abstractTextView sizeThatFits:CGSizeMake(_abstractTextView.frame.size.width, FLT_MAX)];
    _abstractTextView.frameHeight = textViewSize.height;
}
于 2014-06-04T20:20:51.333 回答
1

如果您在其上设置了内容模式,则将 UITextView 添加到其父级后,它似乎会自动调整大小。

这意味着您无需手动计算高度并应用高度约束。它似乎工作!在 iPad 上的 iOS7 和 iOS8 中测试。

例如

--
textView.contentMode = UIViewContentMode.Center;
--

如果有人能解释为什么这会起作用,那将不胜感激。我在弄乱界面生成器中的选项时偶然发现了它。

于 2015-09-07T11:33:48.320 回答
1

只需设置scrollEnabledNO,或取消选中IB 的 Scroll View 部分中的Scrolling EnabledUITextView和will self-size。

于 2018-04-10T16:21:56.397 回答