根据文本内容调整textview(swift)

时间:2016-08-29 07:19:40

标签: swift

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cellFrame = CGRectMake(0, 0, tableView.frame.width, 100)
    var retCell = UITableViewCell(frame: cellFrame)
     var textView = UITextView(frame: CGRectMake(8,30,tableView.frame.width-8-8,65))
                textView.layer.borderColor = UIColor.blueColor().CGColor
                textView.layer.borderWidth = 1
                                    retCell.addSubview(textView)
                                    if(reqGrpIndex == 0){
                    reqGrpIndex = reqGrpIndex + 1
                }
}

func textViewDidChange(textView: UITextView){
    let fixedWidth = textView.frame.size.width
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    var newFrame = textView.frame
    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
    textView.frame = newFrame;
}

我看到一些关于调整textview大小的帖子。但是,在应用这些代码后,我没有这样做。请帮忙。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

将委托设置为uitextview

//If your class is not conforms to the UITextViewDelegate protocol you will not be able to set it as delegate to UITextView
class YourViewController: UIViewController, UITextViewDelegate { 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        textView.delegate = self
    }

    func textViewDidChange(textView: UITextView) { 
        // your resize things
    }
}