当前位置: 首页 > news >正文

iOS_折叠展开 FoldTextView

1. 显示效果

Test1:直接使用:

在这里插入图片描述


Test2:在 cell 里使用:

在这里插入图片描述

2. 使用

2.1 直接使用

// 1.1 init view
private lazy var mooFoldTextView: MOOFoldTextView = {let view = MOOFoldTextView(frame: .zero)view.backgroundColor = .cyanview.mooDelegate = selfreturn view
}()
// 1.2 init conifg
private lazy var attributeTextConfig: MOOFoldTextConfig = {let config = MOOFoldTextConfig()config.allText ="""Swift is a type-safe language, which means the language helps you to be clear about\the types of values your code can work with. If part of your code requires a String,\type safety prevents you from passing it an Int by mistake. Likewise, type safety\prevents you from accidentally passing an optional String"""config.paragraph.lineSpacing = 10.0config.contentWidth = CGRectGetWidth(self.view.bounds)return config
}()// 2.1 add to super view
self.view.addSubview(self.mooFoldTextView)
// 2.2 set config and reload
self.mooFoldTextView.mooConfig = self.mooFoldTextConfig
self.mooFoldTextView.mooReloadText()// 3 set frame
self.mooFoldTextView.frame = CGRect(x: 0.0,y: 100.0,width: CGRectGetWidth(self.view.bounds),height: self.mooFoldTextConfig.mooCurrentHeight())// 4 Implement Proxy
extension MOOTest1ViewController: MOOFoldTextViewDelegate {func mooFoldViewShouldUpdateLayout(_ foldTextView: MOOFoldTextView) {// update layout after fold state changedself.view.setNeedsLayout()}
}         

2.2 在 cell 中使用

2.2.1 at Custom TableViewCell
// 1.1 init
private lazy var attributeTextView: MOOFoldTextView = {let view = MOOFoldTextView(frame: .zero)view.backgroundColor = .cyanview.mooDelegate = selfreturn view
}()
// 1.2 add to super view
self.contentView.addSubview(self.attributeTextView)
// 1.3 set frame
self.attributeTextView.frame = self.contentView.bounds// 2 receive config and set to textView
var mooConfig: MOOFoldTextConfig = MOOFoldTextConfig() {didSet {self.attributeTextView.mooConfig = self.mooConfigself.attributeTextView.mooReloadText()}
}// 3.1 define protocol to forward event
public protocol MOOTableViewCellDelegate: AnyObject {func mooCellShouldReloadData(_ cell: UITableViewCell)
}
// 3.2
weak var mooDelegate: MOOTableViewCellDelegate?
// 3.3
extension MOOTableViewCell: MOOFoldTextViewDelegate {func mooFoldViewShouldUpdateLayout(_ foldTextView: MOOFoldTextView) {self.mooDelegate?.mooCellShouldReloadData(self)}
}
2.2.2 at View Controller
import MOOFoldTextView// 4.1 init tableView
private lazy var tableView: UITableView = {let view = UITableView(frame: .zero, style: .grouped)view.register(MOOTableViewCell.self, forCellReuseIdentifier: "MOOTableViewCell")view.dataSource = selfview.delegate = selfreturn view
}()
// 4.2 init dataSource with config
private lazy var dataSource: [MOOFoldTextConfig] = {let config = MOOFoldTextConfig()config.allText ="""Swift is a type-safe language, which means the language helps you to be clear about\the types of values your code can work with. If part of your code requires a String,\type safety prevents you from passing it an Int by mistake. Likewise, type safety\prevents you from accidentally passing an optional String"""config.paragraph.lineSpacing = 10.0config.contentWidth = CGRectGetWidth(self.view.bounds)return [config]
}()
// 4.3 add to super view
self.view.addSubview(self.tableView)
self.tableView.reloadData()
// 4.4 set frame
self.tableView.frame = CGRect(x: 0.0,y: 100.0,width: CGRectGetWidth(self.view.bounds),height: CGRectGetHeight(self.view.bounds) - 100.0);// 5.1 Implementation UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return self.dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: "MOOTableViewCell",for: indexPath)if let cell = cell as? MOOTableViewCell {cell.mooConfig = self.dataSource[indexPath.row]cell.mooDelegate = self}return cell
}
// 5.2 Implementation UITableViewDelegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {let config = self.dataSource[indexPath.row]return config.currentHeight()
}// 6 reload data after fold state changed
extension MOOTest2ViewController: MOOTableViewCellDelegate {func mooCellShouldReloadData(_ cell: UITableViewCell) {self.tableView.reloadData()}
}

github

http://www.lryc.cn/news/235563.html

相关文章:

  • java使用 TCP 的 Socket API 实现客户端服务器通信
  • conda从4.12升级到最新版23.9 自动升级失败 手动升级方法
  • WPF下实现拖动任意地方都可以拖动窗口
  • Swin Transformer
  • 【csapp lab】lab2_bomblab
  • 开发者分享 | Ascend C算子开发及单算子调用
  • 如何在 Linux 上部署 RabbitMQ
  • 解决更换NodeJs版本后npm -v返回空白
  • 【ES常用查询】基于ElasticsearchRestTemplate及NativeSearchQuery的查询
  • 全志XR806基于http的无线ota功能实验
  • 2023年11月15号期中测验选择题(Java)
  • C# static关键字详解
  • 开发一款回合制游戏,需要注意什么?
  • java的包装类
  • 【数据结构(一)】线性结构和非线性结构
  • 持续集成指南:GitHubAction 自动构建+部署AspNetCore项目
  • Docker 笔记(三)--容器
  • gd32关于IO引脚配置的一些问题
  • QT小记:警告Use multi-arg instead
  • 皮肤性病科专家谭巍主任提出HPV转阴后饮食七点建议
  • 快速弄懂C++中的智能指针
  • C#调用C++ dll教程
  • 计算机毕设 深度学习 大数据 股票预测系统 - python lstm
  • 97.qt qml-自定义Table之实现ctrl与shift多选
  • 运行软件报错mfc140.dll丢失?分享mfc140.dll丢失的解决方法
  • milvus数据库-连接
  • ios + vue3 Teleport + inset 兼容性问题
  • 计蒜客T1654 数列分段(C语言实现)
  • Linux进程——system函数、popen函数
  • 【智能家居】5、主流程设计以及外设框架编写与测试