0

I am new in swift and I am not able to resize the size of textview in collection view cell. I need to resize the height of textview as well as collection view cell height at the same time while editing. My code is like this

class TimeSheetCollectionViewCell1: UICollectionViewCell {
    @IBOutlet var txtviewTimeSheet: UITextView!
    @IBOutlet var btnDelete: UIButton!
    @IBOutlet var txtviewTimeSheetHeight: NSLayoutConstraint!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        txtviewTimeSheet.layer.borderWidth = 0.5
        txtviewTimeSheet.layer.borderColor = #colorLiteral(red: 0.7810397744, green: 0.7810582519, blue: 0.7810482979, alpha: 1)
        txtviewTimeSheet.layer.cornerRadius = 8
        txtviewTimeSheet.autocorrectionType = .no
        let iconUniChar: UniChar = 0xf057
        btnDelete.setTitle(String(format: "%C", iconUniChar), for: .normal)
        btnDelete.setTitleColor(.red, for: .normal)
    }
}

extension TimeSheetViewController:UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView,
                        numberOfItemsInSection section: Int) -> Int {
        return arrcheck.count
    }
    func collectionView(_ collectionView: UICollectionView,
                        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TimeSheetCollectionViewCell1", for: indexPath as IndexPath) as! TimeSheetCollectionViewCell1
        cell.txtviewTimeSheet.text = arrcheck[indexPath.row]
        cell.txtviewTimeSheet.keyboardToolbar.doneBarButton.setTarget(self, action: #selector(doneButtonClicked))
        cell.btnDelete.tag = indexPath.item
        cell.btnDelete.addTarget(self, action: #selector(btnDeleteClick), for: .touchUpInside)
        
        cell.txtviewTimeSheet.delegate = self
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        let label = UITextView(frame: CGRect.zero)
        label.text = arrcheck[indexPath.item]
        label.sizeToFit()
        return CGSize(width: (collectionView.frame.size.width/1) - 10, height: label.frame.height)
    }
    
    @objc func btnDeleteClick(_ sender: UIButton)
    {
        print("buttonPressed ! \(sender.tag)")
        arrcheck.remove(at: sender.tag)
        CollectionViewTimeSheet.reloadData()
    }
    @objc func doneButtonClicked(_ sender: Any) {
        print("Done")
        print("Text Field = ",textFieldIndexPath)
        print("Text Filed Row = ",textFieldIndexPath.row)
        print("Text Filed Section = ",textFieldIndexPath.section)
        print(arrcheck[textFieldIndexPath.row])
        print("Arr = ",arrcheck)
        CollectionViewTimeSheet.reloadData()
    }
}

class TimeSheetViewController: UIViewController ,UITextViewDelegate{
func textViewDidEndEditing(_ textView: UITextView) {
        let cell: UICollectionViewCell = textView.superview?.superview as! UICollectionViewCell
        let table: UICollectionView = cell.superview as! UICollectionView
        textFieldIndexPath = table.indexPath(for: cell)!
        print("Text Field = ",textFieldIndexPath)
        print(textView.text as Any)
        arrcheck[textFieldIndexPath.row] = textView.text!
    }
}

I need to resize textview and collection view cell.Is it possible? If it is possible please share some code. Thanks in Advance!

enter image description here

Muju
  • 884
  • 20
  • 54

0 Answers0