質問

してい UITextView 追加マ UIView.の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];
役に立ちましたか?

解決

利用できる setFrame: または sizeToFit.

更新:

使ってい sizeToFitUILabel, としたもの UITextView がクラスのサブクラス UIScrollView, だったので、その sizeToFit なのです。

また計算のテキストの高さおよび使用 setFrame, がござい UITextView's"スクロールバーの場合は、テキストが長すぎます。

こちらかの文字の高さ:

#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)];

他のヒント

応答があ掲載 どんなサイズUITextViewそのコンテンツ?

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;

注意:っている場合参照オブジェクトのプロパティの多くの時間(例えばフレームやcontentInset)を割り当てをローカルな変数にくいトリガーの追加メソッドの呼び出し(_textView.フレーム/[_textViewフレーム】メソッドの呼び出し).の場合はこのコードの景色は駅の屋根だけという100000sの回では、これが著しく遅(十数メソッドの呼び出しは軽微であり).

しかし...だいたいことは一線のブラウザでクッキーの変数のう

        _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つの別の方法。

textView.contentSize.heighttextViewDidChange できるだけリサイズ テキストで実際に成長したのである。ベスト映像結果よりサイズ変更できます。後の数時間のかを把握しその実現のみならず完全に同じとInstagramでのベストアルゴリズムの中でね)

初期化この:

    // 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)
   {
      //
   }];
}

sizeToFitはない

お問い合わせいただいた場合のsizeToFit後のテキストの設定に初めてリサイズ.その後最初に設定しそれ以降の呼び出しで設定テキストにないサイズ変化でもお問い合わせいただいた場合のsizeToFit.

しかし、できる力でサイズ変更のようになります:

  1. を設定します。
  2. 変更のtextViewフレームの高されCGFLOAT_MAX.
  3. コsizeToFit.

このコンビニエンスストアでのお支ん:

    #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;

次の

_textView.text = someText;
[_textView sizeToFit];
_textView.frame.height = _textView.contentSize.height;

への対応の問題だけで作成され、自動レイアウトに基づく軽量UITextViewサブクラスを自動的に成長-縮小のサイズのユーザー入力できる存在により制約を受けるであろう最大と最小の高さにいなく単一のコンポーネントです。

https://github.com/MatejBalantic/MBAutoGrowingTextView

その答えを与えるには以下のようにGabeくiOS7.1なのでviewDidAppear.マ試験します。

更新:実際、もっと状況は複雑化してきております。を割り当てた場合textView.テキストのresizeTheTextView方法は、iOS7にリサイズ金額をみるフィルタの図式に基づいます。真剣に半端ないです。

UPDATE2:参照 UITextViewコンテンツのサイズの異なるiOS7

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

UPDATE3:

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;
}

後を追加すUITextView親会社の場合 設定モードコンテンツ でないとサイズ変更自体は動きます。

この意味だと思うので、高さは手動で適用さcontraint.思えてならない。試験iOS7、iOS8にいたします。

例えば

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

だって説明できるこの作品でよろしくお願いいたします..なかなる事故が釣オプションのインターフェイスをビルダを構築します。

セットするだけで scrollEnabledNO, チェック スクロール可能 スクロールビューセクションIBの UITextView 自己す。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top