iOS_折叠展开 FoldTextView

1. 显示效果

Test1:直接使用:


Test2:在 cell 里使用:

2. 使用

2.1 直接使用

swift 复制代码
// 1.1 init view
private lazy var mooFoldTextView: MOOFoldTextView = {
    let view = MOOFoldTextView(frame: .zero)
    view.backgroundColor = .cyan
    view.mooDelegate = self
    return 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.0
    config.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 changed
        self.view.setNeedsLayout()
    }
}         

2.2 在 cell 中使用

2.2.1 at Custom TableViewCell
swift 复制代码
// 1.1 init
private lazy var attributeTextView: MOOFoldTextView = {
    let view = MOOFoldTextView(frame: .zero)
    view.backgroundColor = .cyan
    view.mooDelegate = self
    return 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.mooConfig
        self.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
swift 复制代码
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 = self
    view.delegate = self
    return 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.0
    config.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

相关推荐
missmisslulu5 小时前
电容笔值得买吗?2024精选盘点推荐五大惊艳平替电容笔!
学习·ios·电脑·平板
GEEKVIP6 小时前
手机使用技巧:8 个 Android 锁屏移除工具 [解锁 Android]
android·macos·ios·智能手机·电脑·手机·iphone
GEEKVIP6 小时前
如何在 Windows 10 上恢复未保存/删除的 Word 文档
macos·ios·智能手机·电脑·word·笔记本电脑·iphone
奇客软件7 小时前
iPhone使用技巧:如何恢复变砖的 iPhone 或 iPad
数码相机·macos·ios·电脑·笔记本电脑·iphone·ipad
奇客软件1 天前
如何从相机的记忆棒(存储卡)中恢复丢失照片
深度学习·数码相机·ios·智能手机·电脑·笔记本电脑·iphone
GEEKVIP1 天前
如何修复变砖的手机并恢复丢失的数据
macos·ios·智能手机·word·手机·笔记本电脑·iphone
一丝晨光2 天前
继承、Lambda、Objective-C和Swift
开发语言·macos·ios·objective-c·swift·继承·lambda
GEEKVIP2 天前
iPhone/iPad技巧:如何解锁锁定的 iPhone 或 iPad
windows·macos·ios·智能手机·笔记本电脑·iphone·ipad
KWMax2 天前
RxSwift系列(二)操作符
ios·swift·rxswift