大佬教程收集整理的这篇文章主要介绍了根据iOS7中的内容调整UItextview的大小,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
以下代码在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?
但不工作.
任何指针?
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;
- (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,width,height); }
此外,似乎UITextView的setContentSize:方法是从layoutSubviews调用的.所以,在调用ensureLayoutForTextContainer之后,在一个textView(它本身调用layoutSubviews)上调用layoutIfNeeded:在其layoutManager中,应该使textView的contentSize正确.
[someTextView.layoutManager ensureLayoutForTextContainer:someTextView.textContainer]; [someTextView layoutIfNeeded]; // someTextView.contentSize should Now have correct value
以上是大佬教程为你收集整理的根据iOS7中的内容调整UItextview的大小全部内容,希望文章能够帮你解决根据iOS7中的内容调整UItextview的大小所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。