UITextview的高度根据内容而动态变化

时间:2019-03-25 09:12:42

标签: ios swift4

我是iOS新手。

我的任务是从反馈屏幕发送反馈数据。

在“反馈”屏幕中,我有3个字段:

  • 名称文本字段
  • 电子邮件文本字段
  • 反馈文本视图

在UIView中显示。

根据反馈文本视图中的内容,textview的高度应该增加,而textview的高度应该增加或减少时,UIView高度也有所不同。

最主要的是,textview应该增加其高度的两倍,然后执行滚动操作。

我正在使用自动布局来实现这一目标。如果有人可以帮助我实现这一目标,那就太好了。

参考代码:

import UIKit

class FeedbackViewController: UIViewController {

    @IBOutlet weak var feedbacktextview: UITextView!

    @IBOutlet weak var textviewheight: NSLayoutConstraint!


  @IBOutlet weak var containerviewheight: NSLayoutConstraint!
    override func viewDidLoad() {
        super.viewDidLoad()
 self.navigationController?.setNavigationBarHidden(true, animated: false)

    }

}
extension FeedbackViewController: UITextViewDelegate{
    override func didChangeValue(forKey key: String) {
        textviewheight.constant = feedbacktextview.contentSize.height
        containerviewheight.constant = feedbacktextview.frame.size.height
//        let fixedWidth = feedbacktextview.frame.size.width
//        let newSize = feedbacktextview.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
//        feedbacktextview.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)

    }
}

3 个答案:

答案 0 :(得分:1)

为TextView创建插座,并确保选择scrollEnabled为否

import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import * as d3 from 'd3';
import * as d3dag from'd3-dag';


//Defines an interface to pairs of links
interface LinkPairs {
  source: string;
  target: string;
}



@Component({
  selector: 'app-dag',
  encapsulation: ViewEncapsulation.None,  // Stops Angular from renaming class names in the CSS.
  // D3 generates it's own HTML which Angular doesn't know
  // about, so there is a mismatch between class names
  templateUrl: './dag.component.html',
  styleUrls: ['./dag.component.scss']
})
export class DagComponent implements OnInit {


  // Declare and initialise array of tree data
  private linkPairs: LinkPairs[] = [];

  myDag: any;

  constructor() { 

        // Populate 'this.linkPairs' with data

        this.prepareData();

        this.renderDag();
  }

  ngOnInit() {
  }

  prepareData() {

    this.linkPairs.push({source : "1" , target:  "3"});
    this.linkPairs.push({source : "2" , target:  "3"});
    this.linkPairs.push({source : "3" , target:  "4"});
    this.linkPairs.push({source : "4" , target:  "5"});


    console.log(this.linkPairs);

    this.myDag = d3dag.dagConnect(this.linkPairs);

    console.log(this.myDag);

  }

答案 1 :(得分:0)

尝试一下:

首先为UITextView和高度约束创建一个@IBOutlet。然后在代码中动态更改高度后:

class ViewControler: UIViewController, UITextViewDelegate {

    @IBOutlet weak var txtView: UITextView!
    @IBOutlet weak var txtViewHeight: NSLayoutConstraint!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.txtView.delegate = self
    }

    func textViewDidChange(textView: UITextView) {
        let sizeToFitIn = CGSizeMake(self.txtView.bounds.size.width, CGFloat(MAXFLOAT))
        let newSize = self.txtView.sizeThatFits(sizeToFitIn)
        self.txtViewHeight.constant = newSize.height
    }

}

答案 2 :(得分:0)

尝试

@IBOutlet weak var txtView: UITextView!
@IBOutlet weak var constLocationHeight: NSLayoutConstraint!

func textViewDidChange(_ textView: UITextView) {
     adjustFrames(textView, constLocationHeight)        
}

func adjustFrames(_ textView: UITextView, _ constTextview: NSLayoutConstraint)  {
        if textView.contentSize.height >= 80 {
            constTextview.constant = 80
            return;
        } else if textView.contentSize.height < 30 {
            constTextview.constant = 33
            return
        }
        constTextview.constant = textView.contentSize.height
        textView.beginFloatingCursor(at: CGPoint.zero)
        textView.endFloatingCursor()
}