如何根据文本长度使UITextView高度动态?

正如您在此图片中看到的那样

UITextView根据文本长度改变它的高度,我想根据文本长度调整它的高度。

*我看到了其他问题,但那里的解决方案对我不起作用

例

这对我而言,所有其他解决方案都没有。

 func adjustUITextViewHeight(arg : UITextView) { arg.translatesAutoresizingMaskIntoConstraints = true arg.sizeToFit() arg.scrollEnabled = false } 

在Swift 4中, arg.scrollEnabled = false的语法已更改为arg.isScrollEnabled = false

尝试一下:

 CGRect frame = self.textView.frame; frame.size.height = self.textView.contentSize.height; self.textView.frame = frame; 

编辑 – 这是Swift:

 var frame = self.textView.frame frame.size.height = self.textView.contentSize.height self.textView.frame = frame 

接下来是DeyaEldeen的回答。
在我的情况下。 我通过添加自动增长textview高度

迅捷3

textView.translatesAutoresizingMaskIntoConstraints = false textView.isScrollEnabled = false

斯威夫特4

将它添加到您的class级

UITextViewDelegate

 func textViewDidChange(_ textView: UITextView) { let fixedWidth = textView.frame.size.width textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude)) let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude)) var newFrame = textView.frame newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height) textView.frame = newFrame } 

它是以编程方式直接做的。 只需按照这些步骤操作

  1. 添加观察者到文本字段的内容长度

     [yourTextViewObject addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL]; 
  2. 实施观察员

     -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { UITextView *tv = object; //Center vertical alignment CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])/2.0; topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect ); tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect}; mTextViewHeightConstraint.constant = tv.contentSize.height; [UIView animateWithDuration:0.2 animations:^{ [self.view layoutIfNeeded]; }]; } 
  3. 如果你想在输入过程中的一段时间后停止textviewHeight增加,那么实现它并将textview委托设置为self。

     -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if(range.length + range.location > textView.text.length) { return NO; } NSUInteger newLength = [textView.text length] + [text length] - range.length; return (newLength > 100) ? NO : YES; } 

它的工作

 func textViewDidChange(_ textView: UITextView) { let fixedWidth = textviewconclusion.frame.size.width textviewconclusion.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude)) let newSize = textviewconclusion.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude)) var newFrame = textviewconclusion.frame newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height) textviewconclusion.frame = newFrame } 

使用textView.textContainer.maximumNumberOfLines = 10时,iOS 8.3中存在两个陷阱

请参阅我的要点 。

 textView.attributedText = originalContent let lineLimit = 10 textView.isEditable = true textView.isScrollEnabled = false textView.textContainerInset = .zero // default is (8, 0, 8, 0) textView.textContainer.maximumNumberOfLines = lineLimit // Important condition textView.textContainer.lineBreakMode = .byTruncatingTail // two incomplete methods, which do NOT work in iOS 8.3 // size.width可能比maxSize.width小 ————遗憾的是 iOS 8.3 上此方法无视maximumNumberOfLines参数,所以得借助于UILabel // size.width may be less than maxSize.width, ---- Do NOT work in iOS 8.3, which disregards textView.textContainer.maximumNumberOfLines // let size = textView.sizeThatFits(maxSize) // 遗憾的是 iOS 8.3 上此方法失效了,得借助于UILabel // Does not work in iOS 8.3 // let size = textView.layoutManager.usedRectForTextContainer(textView.textContainer).size // Suggested method: use a temperary label to get its size let label = UILabel(); label.attributedText = originalContent let size = label.textRect(forBounds: CGRect(origin: .zero, size: maxSize), limitedToNumberOfLines: lineLimit).size textView.frame.size = size 

1将观察者添加到文本字段的内容长度

  yourTextView.addObserver(self, forKeyPath: "contentSize", options: (NSKeyValueObservingOptions.new), context: nil); 

2实施观察员

 override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { let tv = object as! UITextView; var topCorrect = (tv.bounds.size.height - tv.contentSize.height * tv.zoomScale)/2.0; topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect ); tv.contentOffset.x = 0; tv.contentOffset.y = -topCorrect; self.yourTextView.contentSize.height = tv.contentSize.height; UIView.animate(withDuration: 0.2, animations: { self.view.layoutIfNeeded(); }); } 

SWIFT 4

小费时改变大小

UITextViewDelegate

 func textViewDidChange(_ textView: UITextView) { yourTextView.translatesAutoresizingMaskIntoConstraints = true yourTextView.sizeToFit() yourTextView.isScrollEnabled = false let calHeight = yourTextView.frame.size.height yourTextView.frame = CGRect(x: 16, y: 193, width: self.view.frame.size.width - 32, height: calHeight) } 

加载时更改大小

 func textViewNotasChange(arg : UITextView) { arg.translatesAutoresizingMaskIntoConstraints = true arg.sizeToFit() arg.isScrollEnabled = false let calHeight = arg.frame.size.height arg.frame = CGRect(x: 16, y: 40, width: self.view.frame.size.width - 32, height: calHeight) } 

第二个选项的调用函数如下:

 textViewNotasChange(arg: yourTextView)