我正在尝试根据内容和&amp ;;调整文本视图的大小它也是兄弟姐妹和父容器。
以下代码在iOS 6中正常运行
if (/* less than ios 7 */) {
CGRect frame = _textView.frame;
CGSize conSize = _textView.contentSize;
CGFloat difference = conSize.height - frame.size.height;
frame.size.height += difference;
_textView.frame = frame;
UIScrollView *parentView = (UIScrollView *)_textView.superview;
// adjust views residing below this text view.
// sibling view
UIView *belowView = // access it somehow
CGRect frame1 = belowView.frame;
frame1.origin.y += difference;
belowView.frame = frame1;
// adjust parent scroll view, increase height.
CGSize frame3 = parentView.contentSize;
frame3.height += difference;
parentView.contentSize = frame3;
} else {
// tried
[_textView sizeToFit];
[_textView layoutIfNeeded];
[parentView sizeToFit];
[parentView layoutIfNeeded];
}
试图关注iOS 7解决方案: How do I size a UITextView to its content on iOS 7?
但没有工作。
任何指针?
来自@NSBouzouki的工作代码解决方案
if (/* ios 7 */) {
[_textView.layoutManager ensureLayoutForTextContainer:_textView.textContainer];
[_textView layoutIfNeeded];
}
CGRect frame = _textView.frame;
CGSize conSize = _textView.contentSize;
CGFloat difference = conSize.height - frame.size.height;
frame.size.height += difference;
_textView.frame = frame;
UIScrollView *parentView = (UIScrollView *)_textView.superview;
// adjust views residing below this text view.
// sibling view
UIView *belowView = // access it somehow
CGRect frame1 = belowView.frame;
frame1.origin.y += difference;
belowView.frame = frame1;
// adjust parent scroll view, increase height.
CGSize frame3 = parentView.contentSize;
frame3.height += difference;
parentView.contentSize = frame3;
答案 0 :(得分:26)
似乎UITextView's
contentSize
属性在iOS 7中未正确设置,直到viewDidAppear:
。这可能是因为NSLayoutManager
懒惰地放置了文本,并且必须为contentSize
布置整个文本才是正确的。 ensureLayoutForTextContainer:
方法强制提供文本容器的布局,之后可以使用usedRectForTextContainer:
来获取边界。为了正确获得总宽度和高度,必须考虑textContainerInset
属性。以下方法对我有用。
- (CGRect)contentSizeRectForTextView:(UITextView *)textView
{
[textView.layoutManager ensureLayoutForTextContainer:textView.textContainer];
CGRect textBounds = [textView.layoutManager usedRectForTextContainer:textView.textContainer];
CGFloat width = (CGFloat)ceil(textBounds.size.width + textView.textContainerInset.left + textView.textContainerInset.right);
CGFloat height = (CGFloat)ceil(textBounds.size.height + textView.textContainerInset.top + textView.textContainerInset.bottom);
return CGRectMake(0, 0, width, height);
}
此外,似乎从UITextView's
调用setContentSize:
layoutSubviews
方法。因此,在layoutIfNeeded
上调用textView
之后,layoutSubviews
(其本身称为ensureLayoutForTextContainer:
)上调用layoutManager
,应该textView's
contentSize
更正。
[someTextView.layoutManager ensureLayoutForTextContainer:someTextView.textContainer];
[someTextView layoutIfNeeded];
// someTextView.contentSize should now have correct value
答案 1 :(得分:1)
GrowingTextViewHandler是一个NSObject子类,它在用户键入文本时调整文本视图的大小。 以下是如何使用它。
@interface ViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
@property (strong, nonatomic) GrowingTextViewHandler *handler;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.handler = [[GrowingTextViewHandler alloc]initWithTextView:self.textView withHeightConstraint:self.heightConstraint];
[self.handler updateMinimumNumberOfLines:3 andMaximumNumberOfLine:8];
}
- (void)textViewDidChange:(UITextView *)textView {
[self.handler resizeTextViewWithAnimation:YES];
}
@end