UITableViewCell从内容和垂直居中调整UITextView的大小

时间:2017-08-02 13:06:15

标签: ios uitableview dynamic swift3 height

我获得了UITableView个自定义UITableViewCell。 Autolayout已关闭。 UITableViewCell包含UILabelUITextViewUITextView填充了长度不同的文字。如何动态调整UITextView的高度?我在awakeFromNib中使用此函数尝试了它,并在设置了文本后但没有任何改变。

  func adjustHeight(theTextView: UITextView) {

    let contentSize = theTextView.sizeThatFits(theTextView.bounds.size)
    var frame = theTextView.frame
    frame.size.height = contentSize.height
    theTextView.frame = frame

    let aspectRatioTextViewConstraint = NSLayoutConstraint(item: theTextView, attribute: .height, relatedBy: .equal, toItem: theTextView, attribute: .width, multiplier: theTextView.bounds.height/theTextView.bounds.width, constant: 1)
    theTextView.addConstraint(aspectRatioTextViewConstraint)
}

2 个答案:

答案 0 :(得分:0)

试试这个

yourTextView.numberOfLines = 0

如果文字太长,这会将文本分成几行。

如果这不适合您,并且您只想要1行,则在设置文本后(或在textView的委托函数中)添加此行。

yourTextView.sizeThatFits(yourFrame)

这将使文本更小以适合您为其指定的frme。

希望这有帮助!

答案 1 :(得分:0)

我可以举个例子来解决你的问题。我使用SnapKit https://github.com/SnapKit/SnapKit来更新UIView的约束。这是您要显示表格的UIViewController示例:

class ViewController: UIViewController {

    private var tableView : UITableView = {

        let tabView = UITableView.init(frame: CGRect.zero)
        tabView.register(TableViewCell.self, forCellReuseIdentifier: "cell")
        return tabView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.view.addSubview(self.tableView)
    }

    override func updateViewConstraints() {

        self.tableView.snp.remakeConstraints { (maker) in

            maker.edges.equalTo(self.view) // your table will have the same frame as your view
        }

        super.updateViewConstraints()
    }
}

在第一个扩展程序中,我们将为UITableViewDelegate添加所有方法:

extension ViewController : UITableViewDelegate {

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

        return 200.0 // any value different from zero. 200 is an example. This value must be easy-to-calculate. 
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return UITableViewAutomaticDimension // tells system that rows will be resized automatically
    }
}

在第二个扩展中,有UITableViewDataSource的方法(只生成一些随机数据):

extension ViewController : UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 20
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
        cell.setup(labelText: "Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label Label ", textViewText: "Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text ")
        return cell
    }
}

包含UILabelUITextView的单元格类:

class TableViewCell: UITableViewCell {

    private var label : UILabel = {

        let resultLabel = UILabel.init(frame: CGRect.zero)
        resultLabel.numberOfLines = 0 // If numberOfLines in 0 system will automatically set real number of lines
        return resultLabel
    }()

    private var textView : UITextView = {

        let resultTextView = UITextView.init(frame: CGRect.zero)
        resultTextView.isScrollEnabled = false // disable scroll and editing just for example
        resultTextView.isEditable = false
        return resultTextView
    }()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.contentView.addSubview(self.label)
        self.contentView.addSubview(self.textView)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setup(labelText:String, textViewText:String) {

        self.label.text = labelText
        self.textView.text = textViewText
        self.setNeedsUpdateConstraints()
    }

    override func updateConstraints() {

        self.label.snp.remakeConstraints { (maker) in

            maker.left.equalTo(self.contentView.snp.left)
            maker.right.equalTo(self.contentView.snp.right)
            maker.top.equalTo(self.contentView.snp.top)
            maker.bottom.equalTo(self.textView.snp.top)
        }
        self.textView.snp.remakeConstraints { (maker) in

            maker.left.equalTo(self.contentView.snp.left)
            maker.right.equalTo(self.contentView.snp.right)
            maker.bottom.equalTo(self.contentView.snp.bottom)
        }
        super.updateConstraints()
    }
}