加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 在UITableViewCell中展开UITextView高度(读取更多效果)

发布时间:2020-12-14 17:26:19 所属栏目:百科 来源:网络整理
导读:我正在尝试使用动画扩展UITextView,当用户点击下面阅读更多按钮时. 我已经尝试了很多方法并提出了几乎可行的解决方案: func readMore() { self.tableView.beginUpdates() cell.descTextViewHeightConstraint.constant = cell.descTextView.expectedHeight()
我正在尝试使用动画扩展UITextView,当用户点击下面阅读更多按钮时.

enter image description here

我已经尝试了很多方法并提出了几乎可行的解决方案:

func readMore() {
    self.tableView.beginUpdates()
    cell.descTextViewHeightConstraint.constant = cell.descTextView.expectedHeight()
    self.tableView.endUpdates()
}

UItextView高度

extension UITextView {
    func expectedHeight() -> CGFloat {
        let textView: UITextView = UITextView(frame: CGRect(x: 0,y: 0,width: self.frame.size.width,height: CGFloat.greatestFiniteMagnitude))
        textView.font = self.font
        textView.text = self.text
        textView.sizeToFit()
        return textView.frame.height
    }
}

这个解决方案有效,但有一个副作用 – 当动画结束时,UITableViewCell中的一些元素被隐藏(标题标签).

我也尝试在动画结束时调用tableView.reloadData(),这有效,但有时也有副作用.

解决方法

你为什么不用numberOfLines属性“玩”?

对于一个项目,我做了类似的事情(还有一个旋转的UIImage箭头……).一切都在自定义单元类中:

func setExpanded(_ isExpanded: Bool) {
    if isExpanded {
        lblFullDescription.numberOfLines = 0
        UIView.animate(withDuration: Constants.NavigationBar.AnimationTime,animations: { [weak self] in
            self?.imgArrow.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
        })

    } else {
        lblFullDescription.numberOfLines = 2
        UIView.animate(withDuration: Constants.NavigationBar.AnimationTime,animations: { [weak self] in
            self?.imgArrow.transform = CGAffineTransform(rotationAngle: CGFloat(0))
        })
    }
}

并在包含UITableView的ViewController中添加它:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 42

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读