3

このサイトでいくつかの解決策を読み、それらすべてを試しましたが、まだうまくいきません。現在、私のテストは、その上のシングルviewControllerですUITextViewUITextViewテキストサイズに基づいて高さを調整しようとしています。

良い点: 以下で参照されている解決策は、私に設定する正しい高さを与えているようですUITextView.

リンク

悪い点:理想的な高さに戻した後、UITextView高さを設定できないようです。以下のコードは、これを達成するために使用したいくつかの異なる試みを示しています。現在、3 番目の try メソッドが最も近いように見えますが、それでもまだ高さが固定されていUITextViewます。ここに画像の説明を入力

これは、3 回目の試行からの最新のログの出力です。すべてが機能しているようですが、UITextViewサイズが変更されていません。

2013-10-11 08:27:03.374 textexpand[25273:a0b] The bounds height 872.000000
2013-10-11 08:27:03.377 textexpand[25273:a0b] The frame height 872.000000
2013-10-11 08:27:03.378 textexpand[25273:a0b] The frame height 872.000000
2013-10-11 08:27:03.380 textexpand[25273:a0b] The frame height 785.000000
2013-10-11 08:27:03.381 textexpand[25273:a0b] The bounds height 785.000000

コードは次のとおりです。

- (void)viewDidLoad
{
[super viewDidLoad];

[self thirdtry];

}

////

- (void) firsttry
{
NSString *theText = @"Here is the text I am going to load into the textview.   The text view should automatically expand the height based on the size of the content, but leave the width static.   Also, I would ideally add my own padding to the bottom and top of the uitext view. More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text";

_theTextView.text = theText;

_theTextView.scrollEnabled = NO;
[_theTextView sizeToFit];
[_theTextView layoutIfNeeded];

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

}

////

- (void) secondtry
{
NSString *theText = @"Here is the text I am going to load into the textview.   The text view should automatically expand the height based on the size of the content, but leave the width static.   Also, I would ideally add my own padding to the bottom and top of the uitext view. More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text";

NSAttributedString *attText = [[NSAttributedString alloc] initWithString:theText attributes:nil];
CGRect frame = _theTextView.frame;
CGFloat myfloat = [self textViewHeightForAttributedText:attText andWidth:100];
NSLog(@"The float is %f", myfloat);
frame.size.height = [self textViewHeightForAttributedText:attText andWidth:100];
_theTextView.frame = frame;


}

////

- (void) thirdtry
{

_theTextView.scrollEnabled = NO;

NSString *theText = @"Here is the text I am going to load into the textview.   The text view should automatically expand the height based on the size of the content, but leave the width static.   Also, I would ideally add my own padding to the bottom and top of the uitext view. More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text";

NSAttributedString *attText = [[NSAttributedString alloc] initWithString:theText attributes:nil];

//Attempt to adjust the bounds
CGRect bounds = _theTextView.bounds;
bounds.size.height = [self textViewHeightForAttributedText:attText andWidth:100];
NSLog(@"The bounds height %f", bounds.size.height);
_theTextView.bounds = bounds;

//Attempt to adjust the frame
CGRect frame = _theTextView.frame;
frame.size.height = [self textViewHeightForAttributedText:attText andWidth:100];
_theTextView.frame = frame;
NSLog(@"The frame height %f", frame.size.height);

NSLog(@"The frame height %f", _theTextView.frame.size.height);
[_theTextView sizeToFit];
NSLog(@"The frame height %f", _theTextView.frame.size.height);
}

////

- (CGFloat)textViewHeightForAttributedText: (NSAttributedString*)text andWidth: (CGFloat)width
{
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:text];
CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;

}

アップデート:

このアプローチを使用して、コード内で uitextbox を初期化することにより、目的の効果を得ることができることを追加する必要があります。

CGFloat myfloat= [self textViewHeightForAttributedText:attText andWidth:300];
_theTextView = [[UITextView alloc] initWithFrame:CGRectMake(350, 100, 300, myfloat)];
_theTextView.text = theText;
[_theview addSubview:_theTextView];

ただし、この方法で IB で定義されたフレームをオンザフライで調整するのは避けたいと思います。

4

1 に答える 1