【问题标题】:Set width and height of UITextView based on the dynamic text根据动态文本设置 UITextView 的宽高
【发布时间】:2016-10-20 01:03:43
【问题描述】:

我正在构建一个聊天应用程序,其中每条消息都插入到表格内的一行中。每行包含一个头像和一条消息。我想根据要放入的文本长度设置 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
    }

【问题讨论】:

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

标签: ios swift uilabel uitextview swift3


【解决方案1】:

这两种方法都是根据聊天中的文字来设置textview的高度和宽度。

您可以使用这些。 (斯威夫特 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)

【讨论】:

    【解决方案2】:

    试试这个代码:

    答案 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
     }
    

    【讨论】:

    • 可能是布局问题。请检查您的约束条件。
    【解决方案3】:

    在 viewDidLoad 添加这个

    tableView.estimatedRowHeight = 44.0
    tableView.rowHeight = UITableViewAutomaticDimension
    

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

    【讨论】:

      【解决方案4】:

      将textView的高度约束设置为等于textView的内容大小。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-29
        • 2013-12-26
        • 2013-06-02
        • 1970-01-01
        • 1970-01-01
        • 2017-02-23
        相关资源
        最近更新 更多