简体   繁体   中英

Resize UITextView height to fit text in UITableViewCell

I'm currently in the process of building a social networking app that displays the user's posts in a table. Each post will be displayed in one cell of the table. Because I don't know how much the user will type for one post, I need to set the text view size according to the amount of text typed by the user. The problem is that my current implementation is not working and behaving strangely.

Here is my table code:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    let cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("HomeCell") as? UITableViewCell
    let post : Post = tblData[indexPath.row]

    //Get all custom cell components
    let userLbl = cell?.viewWithTag(101) as UILabel
    let timeLbl = cell?.viewWithTag(102) as UILabel
    let postLbl = cell?.viewWithTag(103) as UITextView
    let profilePic = cell?.viewWithTag(100) as UIImageView
    let postPic = cell?.viewWithTag(104) as UIImageView

    //Post lbl properties
    let textHeight = textViewDidChange(postLbl)
    postLbl.frame.size.height = CGFloat(textHeight)
    //postLbl.selectable = false

    //Individual height variables
    let postInfoHeight = 66 as CGFloat
    var postHeight = 8 + CGFloat(textHeight)
    var imgHeight = 8 + postPic.frame.height as CGFloat

    if post.postInformation == "" {
        postLbl.removeFromSuperview()
        postHeight = 0
    }

    if post.img == nil {
        postPic.removeFromSuperview()
        imgHeight = 0
    }

    //Change the autolayout constraints so it works properly
    return postInfoHeight + postHeight + imgHeight
}

and here is the code that calculates the height of the text view:

func textViewDidChange(textView : UITextView) -> Float {

    let content : NSString = textView.text
    let oneLineSize = content.sizeWithAttributes(["NSFontSizeAttribute": UIFont.systemFontOfSize(14.0)])

    let contentSize = CGSizeMake(textView.frame.width, oneLineSize.height * round(oneLineSize.width/textView.frame.width))

    return Float(contentSize.height)
}

I can't understand why this isn't giving me the correct results. If anyone can spot an error or suggest how to solve this issue I'd really appreciate it. Thanks in advance!

Thanks to @jrisberg for the link to the other question. I decided to abandon using the text view and am now using a UILabel instead. The code in the other question is extremely old and uses a lot of deprecated methods, so I'll post some updated code that works on iOS 8 here. I deleted the textViewDidChange(textView : UITextView) method and changed my tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) method to this:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    let cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("HomeCell") as? UITableViewCell
    let post : Post = tblData[indexPath.row]

    //Get custom cell components
    let postLbl = cell?.viewWithTag(103) as UILabel
    let postPic = cell?.viewWithTag(104) as UIImageView

    //Post lbl properties
    let PADDING : Float = 8
    let pString = post.postInformation as NSString?

    let textRect = pString?.boundingRectWithSize(CGSizeMake(CGFloat(self.tableView.frame.size.width - CGFloat(PADDING * 3.0)), CGFloat(1000)), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(14.0)], context: nil)

    //Individual height variables
    let postInfoHeight = 66 as CGFloat
    var postHeight = textRect?.size.height
    postHeight? += CGFloat(PADDING * 3)
    var imgHeight = 8 + postPic.frame.height as CGFloat

    if post.img == nil {
        postPic.removeFromSuperview()
        imgHeight = 0
    }

    //Change the autolayout constraints so it works properly
    return postInfoHeight + postHeight! + imgHeight
}

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