下午好。
我已经在我的 iOS 应用程序的这个“功能”上苦苦挣扎了几个小时,我需要一些帮助。
问题:我应该如何实现,以便当用户输入UITextView
尺寸增加(仅底部边距)并且单元格增加其高度以适应UITextView
同时动态?我不知道如何解决它。
研究:经过一番搜索后我发现在键入文本时动态更改单元格的高度,并重新加载包含的表格视图以调整大小 https://stackoverflow.com/questions/48851614/dynamically-change-cells-height-while-typing-text-and-reload-the-containing-ta也当 UITableViewCell 被选中时,你能用动画来改变高度吗? https://stackoverflow.com/questions/460014/can-you-animate-a-height-change-on-a-uitableviewcell-when-selected/2063776#2063776我也读过https://developer.apple.com/documentation/uikit/uicollectionview https://developer.apple.com/documentation/uikit/uicollectionview但我觉得到目前为止我的快速知识还不如这些帖子中所期望的。
换句话说,我不知道如何为我的 UICollectionView 实现它。
我的代码现在看起来像这样(我在 EventsCell 中有更多 UI 元素,但将它们剪掉以节省空间,因此它只是UITextView
我想改变)。我已将 RootCell 设置为delegate
for my UITextView
然后我打算在编辑时获取 textView 高度,以获得我想要的高度,但它不起作用。
细胞类别:
class EventsCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.white
setupViews()
}
let textView: GrowingTextView = { // GrowingTextView is a cocoapod extending UITextView
let rootCellDelegate = RootCell()
let tv = GrowingTextView()
tv.backgroundColor = .clear
tv.allowsEditingTextAttributes = true
tv.isScrollEnabled = false
tv.font = UIFont(name: "Didot", size: 16)
tv.textColor = .black
tv.textContainerInset = UIEdgeInsetsMake(4, 4, 4, 6)
tv.placeholder = "Write your event text here"
tv.placeholderColor = UIColor.lightGray
tv.autoresizingMask = .flexibleHeight
tv.isUserInteractionEnabled = false
tv.delegate = rootCellDelegate
return tv
}()
let eventPlaceholderMarkImage: UIButton = {
let iv = UIButton()
let image = UIImage(named: "placeHolderEventTitleMark")
iv.setImage(image, for: .normal)
return iv
}()
func setupViews() {
//MARK: - Constraints for EventsCell
//horizontal
addConstraintsWithFormat(format: "H:|-16-[v0(\(frame.width - 32))]-16-|", views: textView)
addConstraintsWithFormat(format: "V:|-47-[v0]-16-|", views: textView)
addConstraint(NSLayoutConstraint(item: eventPlaceholderMarkImage, attribute: .bottom, relatedBy: .equal, toItem: textView, attribute: .top, multiplier: 1, constant: -7))
addConstraint(NSLayoutConstraint(item: eventPlaceholderMarkImage, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0, constant: 20))
addConstraintsWithFormat(format: "H:|-12-[v0(27)]-\(cellWidth - 39)-|", views: eventPlaceholderMarkImage)
}
}
UICollectionView类:
class RootCell: BaseCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var textViewHeight : CGFloat?
func textViewDidChange(_ textView: UITextView) {
textViewHeight = textView.textContainer.size.height + 63
collectionView.reloadInputViews()
collectionView.reloadData()
collectionView.layoutSubviews()
}
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
cv.delegate = self
cv.dataSource = self
return cv
}()
override func setupViews() {
super.setupViews()
addSubview(collectionView)
addConstraintsWithFormat(format: "H:|[v0]|", views: collectionView)
addConstraintsWithFormat(format: "V:|[v0]|", views: collectionView)
collectionView.register(EventsCell.self, forCellWithReuseIdentifier: EventCellID)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: EventCellID, for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = textViewHeight ?? frame.width * 1/3
let width = frame.width - 8
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
//MARK: - Design selected cells background
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? EventsCell {
cell.textView.isUserInteractionEnabled = true
cell.eventTitleLabel.isUserInteractionEnabled = true
cell.layer.borderColor = UIColor.flatBlack.cgColor
cell.layer.borderWidth = 2
cell.layer.cornerRadius = 7
cell.backgroundColor = GradientColor(.topToBottom, frame: cell.frame, colors: [UIColor.flatRed.withAlphaComponent(0.2), UIColor.white])
} else {return}
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) {
cell.layer.borderColor = UIColor.clear.cgColor
cell.layer.borderWidth = 0
cell.layer.cornerRadius = 0
cell.backgroundColor = .white
} else {return}
}
}
第一次回答后尝试:
class RootCell: BaseCell, UITextViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var textViewText : String?
func textViewDidChange(_ textView: UITextView) {
textViewText = textView.text
DispatchQueue.main.async {
self.collectionView.reloadInputViews()
self.collectionView.reloadData()
}
}
func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
guard let boundingBox = textViewText?.boundingRect(with: constraintRect, options: [NSStringDrawingOptions.usesLineFragmentOrigin], attributes: [NSAttributedStringKey.font: font], context: nil) else {return frame.width * 1/3}
return boundingBox.height
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = frame.width - 8
let height = heightWithConstrainedWidth(width: width, font: UIFont(name: "Didot", size: 16)!)
return CGSize(width: width, height: height)
}
感谢您阅读我的帖子!