UITextView
次のような固有のコンテンツ サイズを返すように サブクラス化しました。
- (void) layoutSubviews
{
[super layoutSubviews];
if (!CGSizeEqualToSize(self.bounds.size, [self intrinsicContentSize])) {
[self invalidateIntrinsicContentSize];
}
}
- (CGSize)intrinsicContentSize
{
/*
Intrinsic content size of a textview is UIViewNoIntrinsicMetric
We have to build what we want here: contentSize + textContainerInset should do the trick
*/
CGSize intrinsicContentSize = self.contentSize;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) {
intrinsicContentSize.width += (self.textContainerInset.left + self.textContainerInset.right ) / 2.0f;
intrinsicContentSize.height += (self.textContainerInset.top + self.textContainerInset.bottom) / 2.0f;
}
return intrinsicContentSize;
}
にオブザーバーを追加しUITextViewTextDidChangeNotification
、テキスト ビューのコンテンツが変更されたときに高さを更新して、テキストの高さに応じて成長させます。
- (void)textViewTextDidChangeNotification:(NSNotification *)notification
{
UITextView *textView = (UITextView *)notification.object;
[self.view layoutIfNeeded];
void (^animationBlock)() = ^
{
self.messageInputViewHeightConstraint.constant = MAX(0, self.messageInputView.intrinsicContentSize.height);
[self.view layoutIfNeeded];
};
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:animationBlock
completion:nil];
}
しかし、テキストビューの高さを埋めるのに十分な行がある場合、新しい行が追加される時間の半分は、この図でわかるように UITextView の NSTextContainer が適切に配置されていません
(NSTextContainer は赤で囲まれ、UITextView は青で囲まれています)
新しい行が追加される残りの半分の時間は、NSTextContainer が正しく置き換えられます。
この奇妙な動作を解決する方法が見つかりませんでした。
あなたの一人がそれを修正するための答えを持っていることを願っています.
ありがとうございました