简体   繁体   English

调整UITextView的高度以适合UITableViewCell中的文本

[英]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. 感谢@jrisberg提供了另一个问题的链接。 I decided to abandon using the text view and am now using a UILabel instead. 我决定放弃使用文本视图,现在改为使用UILabel 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. 另一个问题中的代码非常老,并且使用了许多不赞成使用的方法,因此,我将在此处发布一些适用于iOS 8的更新代码。 I deleted the textViewDidChange(textView : UITextView) method and changed my tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) method to this: 我删除了textViewDidChange(textView : UITextView)方法,并将tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath)方法更改为此:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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