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

时间:2015-06-25 21:00:22

标签: ios swift uitableview autolayout uitextview

我目前正在构建一个社交网络应用,在桌面上显示用户的帖子。每个帖子将显示在表格的一个单元格中。因为我不知道用户将为一个帖子键入多少,我需要根据用户键入的文本量来设置文本视图大小。问题是我当前的实现不起作用并且表现得很奇怪。

这是我的表格代码:

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
}

以下是计算文本视图高度的代码:

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)
}

我无法理解为什么这不能给我正确的结果。如果有人能发现错误或建议如何解决这个问题,我真的很感激。提前谢谢!

1 个答案:

答案 0 :(得分:0)

感谢@jrisberg提供了另一个问题的链接。我决定放弃使用文本视图,现在使用UILabel代替。另一个问题中的代码非常陈旧,并且使用了许多已弃用的方法,所以我将在这里发布一些适用于iOS 8的更新代码。我删除了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
}