Swift:如何在编写UICollectionViewCell高度时使其不断适应UITextView?

时间:2018-08-01 21:12:24

标签: ios swift uicollectionview uitextview uicollectionviewcell

下午好。

我为iOS应用程序的“功能”苦苦挣扎了几个小时,我需要一些帮助。

问题:我应该如何实现,以便当用户键入UITextView时,其大小增加(仅底部边距),并且该单元格的高度增加以适应{{1 }}同时动态地进行?我不知道如何解决。

研究: 经过一番搜索之后,我发现Dynamically change cell's height while typing text, and reload the containing tableview for resizeCan you animate a height change on a UITableViewCell when selected?也都读过https://developer.apple.com/documentation/uikit/uicollectionview,但到目前为止,我的快速见识不如这些文章预期的那样。

换句话说,我无法弄清楚如何为我的UICollectionView实现它。

此刻,我的代码看起来像这样(我在EventsCell中有更多UI元素,但是为了节省空间而将它们切出,因此它只是我要更改的UITextView的底边)。我已将UITextView的RootCell设置为delegate,然后打算在编辑textView高度时获取它,以获取所需的高度,但是它不起作用。

单元格类别:

UITextView

UICollectionView类:

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)
}
}

在第一个答案后尝试:

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}

}
}

感谢您阅读我的帖子!

2 个答案:

答案 0 :(得分:1)

重新加载集合视图或单个单元格将闪烁单元格,并且用户在键入时会失去对TextView的关注。如果用户失去焦点,则必须再次单击UITextView才能继续输入。

当您检测到UITextView需要调整大小时,可以使collectionview的布局无效-如果自动布局参数都正确,这将导致您的单元格被调整大小。请参见下面的示例:

  1. 我有一个UICollectionCell,其中仅包含UITextView,并且已禁用。使用自动版式将UITextView适当地约束到单元的所有边缘。最后,单元格也是UITextView的委托,例如:

    class MyCollectionCell: UICollectionViewCell {
      @IBOutlet private weak var myTextView: UITextView!
    
      //this local variable will be used to determine if the intrinsic content size has changed or not
      private var textViewHeight: CGFloat = .zero
      ...
      ...
      override func awakeFromNib() {
       super.awakeFromNib()
       myTextView.delegate = self
      }
      ...
      ...
    }
    
  2. 将此单元格修改为UITextViewDelegate

    extension MyCollectionCell: UITextViewDelegate { 
    
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
      //this assumes that collection view already correctly laid out the cell
      //to the correct height for the contents of the UITextView
      //textViewHeight simply needs to catch up to it before user starts typing
      let fittingSize = textView.sizeThatFits(CGSize(width: myTextView.frame.width, height: CGFloat.greatestFiniteMagnitude))
      textViewHeight = fittingSize.height
    
      return true
    }
    
    func textViewDidChange(_ textView: UITextView) {
      //flag to determine whether textview's size is changing
      var shouldResize = false
      //calculate fitting size after the content has changed
      let fittingSize = textView.sizeThatFits(CGSize(width: myTextView.frame.width, height: CGFloat.greatestFiniteMagnitude))
    
      //if the current height is not equal to the fitting height
      //save the new fitting height to our local variable and inform the delegate 
      //that collection view needs resizing
      if textViewHeight != fittingSize.height {
        shouldResize = true
        //save the new height 
        textViewHeight = fittingSize.height
      }
    
      //notify the cell's delegate (most likely a UIViewController) 
      //that UITextView's intrinsic content size has changed
      //perhaps with a protocol such as this:
      delegate?.textViewDidChange(newText: textView.text, alsoRequiresResize: shouldResize)
    }
    
  3. 然后,当委托人收到通知时,也许会保存更新的文本并更新如下布局:

    myCollectionView.collectionViewLayout.invalidateLayout()
    

此解决方案的最好之处在于,您不必调用collectionView(_:layout:sizeForItemAt:)。

干杯!

答案 1 :(得分:0)

首先将textView文本存储在某个变量中,然后可以通过此方法获取文本的高度

func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
    let constraintRect = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
    let boundingBox = self.boundingRect(with: constraintRect, options: [NSStringDrawingOptions.usesLineFragmentOrigin], attributes: [NSAttributedStringKey.font: font], context: nil)
    return boundingBox.height
}

然后将此高度传递给单元格+填充物 并且不要忘记在用户输入内容时重新加载您的collectionView