Swift:如何在写入时不断调整 UICollectionViewCell 高度以适应 UITextView?

2024-03-19

下午好。

我已经在我的 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)

}

感谢您阅读我的帖子!


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

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

  1. 我有一个 UICollectionCell,里面只有 UITextView,在 UITextView 上滚动是disabled。使用 AutoLayout 将 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:)。

Cheers!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swift:如何在写入时不断调整 UICollectionViewCell 高度以适应 UITextView? 的相关文章

随机推荐

  • Marathon 上的临时任务

    在此之前 我要说的是 我是 Mesosphere 堆栈的新手 我正在尝试将现有的 Rails 应用程序部署迁移到 Mesos 到目前为止我已经成功 但目前我正在运行迁移和种子 通过 Rake 任务 我没有看到一个很好的方法来实现它因为这些任
  • Linux 操作系统:/proc/[pid]/smaps 与 /proc/[pid]/statm

    我想计算单个进程的内存使用情况 经过一些研究后 我发现了 smaps 和 statm 首先什么是smaps和statm 有什么不同 statm 有一个 RSS 字段 在 smaps 中我总结了所有 RSS 值 但对于同一过程 这些值是不同的
  • 如何在 Vim 上将 inoremap/nnoremap 设置为“命令键”?

    我想将操作分配给 Vim 中的命令键 难道就没有什么方法吗 假设你使用的是 Mac 你无法在 vim 中映射命令键当它在终端模拟器中运行时 仅在 MacVim 中GUI 如果您使用 MacVim GUI 您的映射应该使用
  • Memcache获取密钥过期时间

    使用 memcached 和 php memcached 库 有没有办法在执行操作时获取当前密钥到期时间get http www php net manual en memcache get php 使用这个例子 它显示所有服务器密钥及其过
  • 需要在 python 测试用例中模拟一些基类行为

    我的标题相当具有描述性 但就这样吧 假设我有这个设置 class BaseClass object def init self pass def base function self param Hello World print para
  • Angular 控制台仅从 main.js:1 和 polyfills.js:1 记录日志

    我正在构建一个新的角度应用程序 在我执行 console log 的任何地方 它都来自 main js 1 或 polyfills js 1 而不是来自正确的组件 我尝试将 main js 和 polyfills js 添加到开发工具设置中
  • 如何从 Excel 获取输入并使用该值在 as400 中运行宏

    今天我正在研究as400宏 我需要对3000 条记录一一运行相同的宏 Excel 工作表在 A 列中以数字格式包含 3000 多行 每次我需要运行宏时 在此之前我需要使用 Excel A 列中的值更改 AS400 宏代码 下面是 AS400
  • 显示 NLTK 中的标签概率/置信度

    我正在使用 Python NLTK 库中的 MaxEnt 分类器 对于我的数据集 我有许多可能的标签 并且正如预期的那样 MaxEnt 仅返回一个标签 我已经训练了我的数据集并获得了大约 80 的准确率 我还在未知数据项上测试了我的模型 结
  • CSS 仅在 Div 中的其他图像保持饱和的情况下降低背景图像的饱和度

    我试图降低 div 背景图像的饱和度 同时使同一 div 内的图像保持饱和 我找到了一个与我想要做的事情接近的例子 除了模糊 但尝试了它的多种变体但没有成功 在下面的代码中 desaturate 类正确应用滤镜属性并将背景图像转换为黑白图像
  • 如何将 PHP 标签与正则表达式匹配? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我不太擅长正则表达式 你能给我一个匹配任何 php 标签的模式吗 is php blocks print r blocks 一个问题是 如
  • 使用 Jenkins 构建在 Docker 容器内运行的 Docker 镜像

    我正在读书本文 http container solutions com running docker in jenkins in docker 关于设置 Jenkins 在 Docker 容器内运行 以便 Jenkins 能够自行构建 D
  • WinInet如何使用sock代理

    我想使用 WinInet 访问网页 但使用ocks4 socks5 代理 这是我尝试过的 HINTERNET hOpen InternetOpenA helper USER AGENT INTERNET OPEN TYPE PROXY SO
  • 通过 SSH 远程运行脚本

    我需要从 100 个远程服务器收集用户信息 我们有用于身份验证的公钥 私钥基础设施 并且我已经配置了ssh agent命令转发密钥 这意味着我可以在没有密码提示的情况下登录任何服务器 自动登录 现在我想在所有服务器上运行一个脚本来收集用户信
  • NSMenuItem 未快速启用

    我的项目中有一个 NSMenuItem var statusBar NSStatusBar systemStatusBar var statusItem NSStatusItem NSStatusItem var menuItem NSMe
  • 在 iOS 中使用 Microsoft App Center 环境变量

    我想开源我的 iOS 应用程序 但仍然能够使用 Microsoft App Center 等 CI 系统来构建它 我的应用程序使用我想从开源存储库中隐藏的私有 API 密钥 幸运的是 App Center 提供了环境变量功能 旨在在构建时使
  • OpenSSL 错误消息:错误:0A000126:SSL 例程::读取时出现意外的 eof

    截图在这里 https i stack imgur com 45QV6 png 我使用 YII2 框架用 PHP 构建了一个网站 当我使用 file get contents requestUrl false stream context
  • 在我的所有 vue 组件中全局使用 axios

    我正在 Vue 应用程序和 CLI 中使用 axios 进行测试 我一直在使用 vue resource 只需将其传递给 Vue use VueResource 即可在所有组件上访问它 如何使用 axios 实现此目的 这样我就不必将其导入
  • 如何获取 Flutter 应用程序的屏幕尺寸?

    我以全页模式显示从云存储加载的图像 我可以向图像 URL 提供一个参数来请求特定大小 例如匹配最长的屏幕尺寸 这样 如果屏幕只有 1334 像素 我就不必加载 1920px 图像 如何获取屏幕尺寸作为参数传递 您可以使用MediaQuery
  • mongodb 组和子组计数

    我有以下文档类型 id fA2845ORqeyMUItKXfqZw user 553247ffdc8a4ade4bb09c5e state 2 metadata language en US pipeline api http localh
  • Swift:如何在写入时不断调整 UICollectionViewCell 高度以适应 UITextView?

    下午好 我已经在我的 iOS 应用程序的这个 功能 上苦苦挣扎了几个小时 我需要一些帮助 问题 我应该如何实现 以便当用户输入UITextView尺寸增加 仅底部边距 并且单元格增加其高度以适应UITextView同时动态 我不知道如何解决