根据动态文本设置UITextView的宽度和高度

3
我正在构建一个聊天应用程序,其中每个消息都会插入到表格中的一行中。每行包含一个头像和一条消息。我想根据要放置的文本长度设置UITextArea的宽度和高度。
以下是我使用的代码。但在这里,高度和宽度都是固定的(200x50)
PS:我是Swift和iOS的新手,我正在使用Swift 3。我搜索到的所有代码都是objective-c或swift 2
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell  = Bundle.main.loadNibNamed("ChatBox1", owner: self, options: nil)?.first as! ChatBox1


        let myTextField: UITextView = UITextView(frame: CGRect(x: 30, y: 20, width: 200.00, height: 50.00));
        cell.addSubview(myTextField)
        myTextField.isScrollEnabled = false
        myTextField.isUserInteractionEnabled = false
        myTextField.backgroundColor = UIColor.lightGray
        myTextField.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
        myTextField.layer.cornerRadius = 5
        print(myTextField.intrinsicContentSize)


        let image = UIImage(named: "agent")
        let imageView = UIImageView(image: image)
        imageView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        cell.addSubview(imageView)


        return cell
    }

enter image description here


1
尝试使用cell.sizeToFit()。 - Joe
请查看 boundingRect(with:options:attributes:context:),这是 NSString 的一个方法。您可以使用此方法计算整个字符串的大小,然后相应地设置 textView 和单元格高度。 - Eric Qian
1
尝试使用cell.sizeToFit(),并在您的行数之内尝试tableView.estimatedRowHeight = 44.0 tableView.rowHeight = UITableViewAutomaticDimension。这将解决您的问题。 - Joe
1
与您的文本字段无关。请查看我的其他评论,那就是您要寻找的答案... - Joe
1
在你的 func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { 中,返回 yourArray.count }。 - Joe
显示剩余5条评论
4个回答

2

这两种方法是用来根据聊天中的文本设置TextView的高度和宽度。

您可以使用以下代码。(Swift 2.3)

var screenrect = UIScreen.mainScreen().bounds

func GET_WIDTH(textView : UITextView , text : String)-> CGFloat
    {
        textView.text = text;
        let size = textView.bounds.size
        let newSizeWidth = textView.sizeThatFits(CGSize(width: CGFloat.max, height: size.height))
        // Resize the cell only when cell's size is changed
        if size.width != newSizeWidth.width
        {
            if( newSizeWidth.width > screenrect.width-120 )
            {
                textView.layer.frame.size.width = screenrect.width-120;

            }
            else if ( newSizeWidth.width < 40 )
            {
                textView.layer.frame.size.width = 40
            }
            else
            {
                textView.layer.frame.size.width = newSizeWidth.width;
            }
        }
        return textView.layer.frame.size.width + 40;
    }

    func GET_HEIGHT(textView : UITextView , text : String)-> CGFloat
    {
        textView.text = text;
        let size2 = textView.bounds.size
        let newSize = textView.sizeThatFits(CGSize(width: size2.width, height: CGFloat.max))
        if size2.height != newSize.height
        {
            if( newSize.height < 40 )
            {
                textView.layer.frame.size.height = 40
            }
            else
            {
                textView.layer.frame.size.height = newSize.height
            }
        }
        return textView.layer.frame.size.height + 15;
    }

您需要这样调用textview的函数来设置高度和宽度。
 let textViewRight=UITextView(frame: CGRectMake(0, 4, 40, 40));
 textViewRight.layer.frame.size.width = GET_WIDTH(textViewRight, text: currentObject.chat_userMessage)
 textViewRight.layer.frame.size.height = GET_HEIGHT(textViewRight, text: currentObject.chat_userMessage)

1
尝试以下代码:

Answer 1:

   func tableView(tableView:UITableView!, numberOfRowsInSection section: Int) -> Int { 

        yourTableViewName.estimatedRowHeight = 44.0 // Standard tableViewCell size
        yourTableViewName.rowHeight = UITableViewAutomaticDimension

   return yourArrayName.count } 


   And also put this code inside your Cell for incase...
   yourCell.sizeToFit() 

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

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

可能是布局问题,请检查您的约束条件。 - Joe

0

获取textView的高度约束并将其设置为textView的内容大小。

或者,如果您使用自动布局,可以将内容吸附和压缩属性设置为1000。它应该根据内容扩展单元格。


0

在viewDidLoad中添加以下内容

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

只需添加这两行代码,您的问题就会得到解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接