How to adjust the height of a textview to his content in SWIFT?

前端 未结 7 733
无人及你
无人及你 2020-12-07 23:05

In my view controller, I have an UITextView. This textview is filled with a string. The string can be short or long. Depending on the length of the string, the height of the

相关标签:
7条回答
  • 2020-12-07 23:34

    Why not have an image for the back button and a UILabel for your text, and use autolayout to position them? And maybe a third view for the yellow background.

    0 讨论(0)
  • 2020-12-07 23:43

    My simple solution to an extremely similar problem:

    I used labels with the number of lines set to 0 instead of trying to use text fields or views. Then I clipped the leading and trailing edges to the labels' containers which constrain their widths in my case, with 0 as the constant for the constraints. The catch is to set the number of lines to 0 to allow the labels to grow or shrink according to their content. The text never gets chopped and the height of the labels never exceed their contents' for all of the different size classes I configured for the widths (the container widths in my case). Tested for IOS 8.0 and later (also make sure 'explicit' is unchecked next to 'Preferred Width' attributes for all lables for the label widths to adjust for different size classes) - works perfectly.

    0 讨论(0)
  • 2020-12-07 23:46

    First, disable UITextView's scrollable. Two options:

    1. uncheck Scrolling Enabled in .xib.
    2. [TextView setScrollEnabled:NO];

    Create a UITextView and connect it with IBOutlet (TextView). Add a UITextView height constraint with default height, connect it with IBOutlet (TextViewHeightConstraint). When you set your UITextView’s text asynchronously you should calculate the height of UITextView and set UITextView’s height constraint to it. Sample code:

    CGSize sizeThatFitsTextView = [TextView sizeThatFits:CGSizeMake(TextView.frame.size.width, MAXFLOAT)];
    
    TextViewHeightConstraint.constant = sizeThatFitsTextView.height; 
    
    0 讨论(0)
  • 2020-12-07 23:48

    In swift 2.3:

     func textViewDidChange(textView: UITextView) {
    
        let size = textView.sizeThatFits(CGSize(width: textView.frame.size.width, height: CGFloat.max))
        if size.height != TXV_Height.constant && size.height > textView.frame.size.height{
            TXV_Height.constant = size.height
            textView.setContentOffset(CGPointZero, animated: false)
        }
    }
    
    0 讨论(0)
  • 2020-12-07 23:51

    In Swift 3.0, where tvHeightConstraint is the height constraint of the subject TextView (if using for multiple textviews, would add it to the function inputs):

    func textViewDidChange(textView: UITextView) {
    
            let size = textView.sizeThatFits(CGSize(width: textView.frame.size.width, height: CGFloat.greatestFiniteMagnitude))
            if size.height != tvHeightConstraint.constant && size.height > textView.frame.size.height{
                tvHeightConstraint.constant = size.height
                textView.setContentOffset(CGPoint.zero, animated: false)
            }
        }
    

    H/T to @cmi and @Pablo Ruan

    0 讨论(0)
  • 2020-12-07 23:55

    I have adapted your code and used it for both: layout subview and textViewDidChange. Thank you very much that made my day after an hour of troubleshooting...

    override func viewDidLayoutSubviews() {
    
        super.viewDidLayoutSubviews()
        textViewDidChange(self.recordingTitleTV)
    
    
    }
    
    func textViewDidChange(_ textView: UITextView) {
    
        let contentSize = textView.sizeThatFits(textView.bounds.size)
        var frame = textView.frame
        frame.size.height = contentSize.height
        textView.frame = frame
    
        let aspectRatioTextViewConstraint = NSLayoutConstraint(item: textView, attribute: .height, relatedBy: .equal, toItem: textView, attribute: .width, multiplier: textView.bounds.height/textView.bounds.width, constant: 1)
        textView.addConstraint(aspectRatioTextViewConstraint)
    }
    
    0 讨论(0)
提交回复
热议问题