首页 > 解决方案 > Swift:ReadMoreTextView 库点击检测

问题描述

您好,我目前正在使用开源 ReadMoreTextView 库来扩展我的描述。如何仅在点击“阅读更多”或“...阅读少”时进行检测。目前在使用这些函数,hitTest 和 points 时,所有的 textView 都会被检测到。我的目标是,当我选择“阅读更多”时,我的 textView 和 tableView 标题将在其“阅读更少”时展开或折叠。这两个功能可以帮助但不知道什么是正确的方法。非常感谢!

let descriptionTextView: ReadMoreTextView = {
    let textView = ReadMoreTextView()
    let readMoreTextAttributes: [NSAttributedString.Key: Any] = [
        NSAttributedString.Key.foregroundColor: UIColor.red,
        NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16)
    ]
    let readLessTextAttributes = [
        NSAttributedString.Key.foregroundColor: UIColor.red,
        NSAttributedString.Key.font: UIFont.italicSystemFont(ofSize: 16)
    ]
    textView.attributedReadMoreText = NSAttributedString(string: "... Read more", attributes: readMoreTextAttributes)
    textView.attributedReadLessText = NSAttributedString(string: " Read less", attributes: readLessTextAttributes)
    textView.backgroundColor = .clear
    textView.textColor = .white
    textView.shouldTrim = true
    textView.maximumNumberOfLines = 3
    return textView
}()


override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

}

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {

    return true
}

标签: swiftxcode

解决方案


我也面临同样的问题。在与 ReadMoreTextView 库斗争之后,我使用了不同的库ExpandableLabel库,它解决了在更多按钮单击时获得回调的问题。希望这也能解决您的问题。


推荐阅读