如何根据具有打字机效果的UILabel设置单元格的高度

cmi*_*mii 5 animation uitableview tableviewcell ios swift

我在UITableView的单元格中有一个UILabel.

要根据标签的高度调整单元格的高度,没关系,它可以完美地工作.

但我需要添加另一个约束.我需要用打字机效果显示UILabel(逐个字母).

我对该效果的扩展效果很好:

extension UILabel{

    func setTextWithTypeAnimation(id:String, typedText: String, pauseCharacterArray: [Int:Double], characterInterval: TimeInterval = 0.06 ) {

        text = ""

        let group = DispatchGroup()
        group.enter()

        DispatchQueue.global(qos: .userInteractive).async {

            for (index, character) in typedText.characters.enumerated() {

                DispatchQueue.main.async {
                    self.text = self.text! + String(character)
                }

                Thread.sleep(forTimeInterval: characterInterval)
            }

        group.leave()
    }

    group.notify(queue: .main) {
        //do something
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图在configureCell基金中调用此函数:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "ParagraphTableViewCell", for: indexPath) as! ParagraphTableViewCell

    cell.delegate = self
    self.configureCell(cell: cell, atIndexPath: indexPath)
    return cell
 }


func configureCell(cell: ParagraphTableViewCell, atIndexPath indexPath: IndexPath) {

    let paragraph = paragraphArray[indexPath.row] as! Paragraph

    let pauseCharactersArray:[Int:Double] = [1:0, 6:0]
    cell.dialogueLabel.setTextWithTypeAnimation(id:"intro", typedText: "lorem ipsum", pauseCharacterArray: pauseCharactersArray)
}
Run Code Online (Sandbox Code Playgroud)

但标签没有出现.我认为这是因为单元格中标签的高度设置为0,并且它永远不会更新.

我不知道如何调整"现场"单元格的高度(每次显示一个字符时)

编辑

我使用autolayout

编辑2

打字机效果,在没有UITableView的简单UILabel中运行:

GIF

xib中设置的单元格:

厦门国际银行

当我跑步时,UILabel没有出现:

跑

小智 4

我能够使用不同的方法来实现。

  • 我创建了一个堆栈视图来对标签和按钮进行分组(以及一个子堆栈视图来排列按钮)。
  • 在按钮的堆栈视图上,我通过 IB 固定了高度。
  • 在父堆栈视图上。我将堆栈视图固定到单元格的边距。
  • 在标签上,我固定了两侧以适应堆栈视图(不要固定底部,您可以自由固定顶部)

这是我的IB 屏幕截图,可供参考。

  • 在 ViewController 上,设置以下属性:

    yourTableView.rowHeight = UITableViewAutomaticDimension
    yourTableView.rowHeight.estimatedRowHeight = 40 // You should set an initial estimated row height here, the number 40 was chosen arbitrarily
    
    Run Code Online (Sandbox Code Playgroud)
  • 我编辑了你的方法来添加回调。

    func setTextWithTypeAnimation(id:String, typedText: String, pauseCharacterArray: [Int:Double], characterInterval: TimeInterval = 0.06, callBackAfterCharacterInsertion:(()->())?) {
    
      text = ""
    
      let group = DispatchGroup()
      group.enter()
    
      DispatchQueue.global(qos: .userInteractive).async {
    
        for (_, character) in typedText.characters.enumerated() {
    
            DispatchQueue.main.async {
                self.text = self.text! + String(character)
                callBackAfterCharacterInsertion?()
            }
    
            Thread.sleep(forTimeInterval: characterInterval)
        }
    
        group.leave()
      }
    
      group.notify(queue: .main) {
        //do something
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 通过回调,我在每个字符更新后从 TableView调用beginUpdates()and 。endUpdates()

希望这对您有任何帮助。