简体   繁体   中英

how to increase the textview size according to text in static table view in swift 3

I have 3 custom cells in Static TableView. All cells have different heights. But I need to make height of newsDetail TextView dynamic according to its content size. It was easy to make dynamic for label in tableview. But it didn't work out for TextView in Static TableView. I searched but couldn't find proper solution for this, anyone please could help me in swift 3? My codes:

import UIKit



class DetailTableView: UITableViewController {

@IBOutlet weak var myImageView: UIImageView!


@IBOutlet weak var newsHeadline: UILabel!


@IBOutlet weak var newsDetail: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()

   newsDetail.text = "Barack Hussein Obama II (US: /bəˈrɑːk huːˈseɪn oʊˈbɑːmə/ (About this sound listen) bə-RAHK hoo-SAYN oh-BAH-mə;[1][2] born August 4, 1961) is an American politician who served as the 44th President of the United States from 2009 to 2017. He is the first African American to have served as president. He previously served in the U.S. Senate representing Illinois from 2005 to 2008 and in the Illinois State Senate. Obama was born in 1961 in Honolulu, Hawaii, two years after the territory was admitted to the Union as the 50th state. Raised largely in Hawaii, Obama also spent one year of his childhood in Washington State and four years in Indonesia. After graduating from Columbia University in 1983, he worked as a community organizer in Chicago. In 1988 Obama enrolled in Harvard Law School, where he was the first black president of the Harvard Law Review. After graduation, he became a civil rights attorney and professor, and taught constitutional law at the University of Chicago Law School from 1992 to 2004. Obama represented the 13th District for three terms in the Illinois Senate from 1997 to 2004, when he ran for the U.S. Senate."


    tableView.estimatedRowHeight = 44
    tableView.rowHeight = UITableViewAutomaticDimension


}

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 3
}

public override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
}

First you need to make sure that scroll is disabled for your UITextView. commentsTextView.isScrollEnabled = false

Then, set the autolayout constraints in your storyboard for your UITextView, but make sure to not set one for it's height.

You should implement UITableViewDataSource methods heightForRowAt and estimatedHeightForRowAt or use the UITableViewController corresponding attributes.

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 77
}

If your UITextView is editable, and you want it to resize dynamically while you're typing, make your controller it's delegate, and implement textViewDidChange as following:

extension ContactDetailsViewController: UITextViewDelegate {

// Resize textview depending on it's content
func textViewDidChange(_ textView: UITextView) {

    // Get textview content size
    let size = textView.bounds.size
    let newSize = textView.sizeThatFits(CGSize(width: size.width, height: .greatestFiniteMagnitude))

    // Resize the cell only when cell's size is changed
    if size.height != newSize.height {
        UIView.setAnimationsEnabled(false)
        tableView.beginUpdates()
        tableView.endUpdates()
        UIView.setAnimationsEnabled(true)

        let indexPath = IndexPath(row: 2, section: 0)
        tableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
    }
}

Your TextView wont make your cell height bigger, since textView will grow in his content size. I mean it increase his scroll and not its height. So if your textView does not increase his height, neither will your dynamic cell height.

Solution: use a label with number of row = 0

You can use PTSMessagingCell

Add PTSMessagingCell.h and PTSMessagingCell.m file in your resources.

#import "PTSMessagingCell.h" in your header file.

Then implement UITableViewDataSource method heightForRowAt as below:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    let textSize = PTSMessagingCell.messageSize(newsDetail.text)
    return textSize.height + 2*PTSMessagingCell.textMarginVertical() + 5.0
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM