iOS UITextView Inner UITableViewCell“;阅读更多“;就像在现代应用商店一样
几天来,我一直在为如何正确实现iOS应用商店中的“阅读更多”行为而苦苦挣扎(附屏幕截图) 我正在项目中使用iOS UITextView Inner UITableViewCell“;阅读更多“;就像在现代应用商店一样,ios,swift,uitableview,uitextview,expandable,Ios,Swift,Uitableview,Uitextview,Expandable,几天来,我一直在为如何正确实现iOS应用商店中的“阅读更多”行为而苦苦挣扎(附屏幕截图) 我正在项目中使用AutoLayout。UITableView行正在通过以下方式使用行的自调整高度:tableView.rowHeight=UITableView.automaticDimension 我尝试了很多方法,通过UITextView属性化字符串,手动截断它并重新发出布局请求。我最后一次尝试是尝试使用textView.textContainer.ExclutionPaths。这是我到目前为止得到的
AutoLayout
。UITableView
行正在通过以下方式使用行的自调整高度:tableView.rowHeight=UITableView.automaticDimension
我尝试了很多方法,通过UITextView
属性化字符串,手动截断它并重新发出布局请求。我最后一次尝试是尝试使用textView.textContainer.ExclutionPaths
。这是我到目前为止得到的代码:
import UIKit
class ReadMoreTextViewCell: UITableViewCell {
static var mediumFont: UIFont {
return UIFont.systemFont(ofSize: 16)
}
private let textView = UITextView()
private let button: UIButton = {
let button = UIButton(type: .custom)
return button
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
setupConstraints()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupViews()
setupConstraints()
}
private func setupViews() {
textView.isScrollEnabled = false
textView.textContainer.lineFragmentPadding = 0.0
textView.textContainerInset = .zero
textView.backgroundColor = UIColor.clear
textView.textColor = .black
textView.textContainer.maximumNumberOfLines = 3
textView.textContainer.lineBreakMode = .byTruncatingTail
contentView.addSubview(textView)
button.setTitle("Read more", for: .normal)
button.titleLabel?.font = ReadMoreTextViewCell.mediumFont
button.setTitleColor(UIColor.blue, for: .normal)
button.addTarget(self, action: #selector(readMoreTapped), for: .touchUpInside)
button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -6, right: 0)
contentView.addSubview(button)
}
private func setupConstraints() {
textView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
textView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
textView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
textView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor),
textView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor)
])
}
override func layoutSubviews() {
super.layoutSubviews()
contentView.layoutIfNeeded()
let buttonFrame = CGRect(x: textView.bounds.width - 80, y: textView.bounds.height - 26, width: 80, height: 26)
button.frame = buttonFrame
let buttonPath = UIBezierPath(rect: button.frame)
textView.textContainer.exclusionPaths = [buttonPath]
}
override func prepareForReuse() {
super.prepareForReuse()
textView.delegate = .none
textView.attributedText = .none
}
func setup(with text: String) {
textView.attributedText = text.attributedString(font: UIFont.systemFont(ofSize: 16))
}
@objc func readMoreTapped() {
}
}
结果如下面的屏幕截图所示:
刷新单元格后通过拉出可见的UITableView
区域:
最后,我需要在这里使用UITextView
。
如果您在某些项目中解决了此问题,请提供帮助:-)如果您只是想获得“阅读更多”的正确位置,您可以执行以下操作。
但这并未考虑以下因素:
button.contentEdgeInsets=UIEdgeInsets(顶部:0,左侧:0,底部:-6,右侧:0)
并添加了button.backgroundColor=.white
回到代码,更新您在按钮上设置约束的方式。执行以下操作,而不是使用frame设置它们
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
button.lastBaselineAnchor.constraint(equalTo: textView.lastBaselineAnchor),
button.heightAnchor.constraint(equalToConstant: 20),
button.widthAnchor.constraint(equalToConstant: 80),
button.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor)
])
基于以上,我得到了这个结果
以上内容仍可以稍作调整,但如果我理解您的问题,这是一个快速修复方法,可以帮助您走上正确的道路。也许会有所帮助。@rmaddy谢谢,但我已经看到了,这不是我的情况/对我不起作用,因为我在
UITableViewCell
中。如果显示read more且文本被截断,高度应该是多少?