MultilineTextFieldCell.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import Foundation
  2. import UIKit
  3. class MultilineTextFieldCell: UITableViewCell, UITextViewDelegate {
  4. static let cellHeight: CGFloat = 125
  5. var onTextFieldChange:((_:UITextView) -> Void)? // set this from outside to get notified about textfield changes
  6. lazy var descriptionField: UITextField = {
  7. let textField = UITextField()
  8. textField.translatesAutoresizingMaskIntoConstraints = false
  9. textField.isEnabled = false
  10. return textField
  11. }()
  12. lazy var textField: UITextView = {
  13. let textField = UITextView()
  14. textField.delegate = self
  15. textField.translatesAutoresizingMaskIntoConstraints = false
  16. textField.font = UIFont.systemFont(ofSize: 16)
  17. return textField
  18. }()
  19. lazy var placeholder: UILabel = {
  20. let placeholderLabel = UILabel()
  21. placeholderLabel.font = self.textField.font
  22. placeholderLabel.textColor = UIColor.lightGray
  23. placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
  24. return placeholderLabel
  25. }()
  26. init(description: String, multilineText: String?, placeholder: String) {
  27. super.init(style: .value1, reuseIdentifier: nil)
  28. self.descriptionField.text = "\(description):"
  29. self.textField.text = multilineText
  30. self.placeholder.text = placeholder
  31. self.placeholder.isHidden = !textField.text.isEmpty
  32. selectionStyle = .none
  33. setupViews()
  34. }
  35. required init?(coder _: NSCoder) {
  36. fatalError("init(coder:) has not been implemented")
  37. }
  38. func setupViews() {
  39. contentView.addSubview(descriptionField)
  40. contentView.addSubview(textField)
  41. contentView.addSubview(placeholder)
  42. let margins = contentView.layoutMarginsGuide
  43. descriptionField.alignLeadingToAnchor(margins.leadingAnchor)
  44. descriptionField.alignTrailingToAnchor(margins.trailingAnchor)
  45. descriptionField.alignTopToAnchor(margins.topAnchor)
  46. textField.alignLeadingToAnchor(margins.leadingAnchor, paddingLeading: -5)
  47. textField.alignTrailingToAnchor(margins.trailingAnchor)
  48. contentView.addConstraint(textField.constraintHeightTo(95))
  49. textField.alignTopToAnchor(descriptionField.bottomAnchor)
  50. placeholder.alignLeadingToAnchor(margins.leadingAnchor)
  51. placeholder.alignTrailingToAnchor(textField.layoutMarginsGuide.trailingAnchor)
  52. placeholder.alignTopToAnchor(textField.layoutMarginsGuide.topAnchor)
  53. }
  54. override func setSelected(_ selected: Bool, animated _: Bool) {
  55. if selected {
  56. textField.becomeFirstResponder()
  57. }
  58. }
  59. func getText() -> String? {
  60. return textField.text
  61. }
  62. func setText(text: String?) {
  63. textField.text = text
  64. }
  65. // MARK: - UITextViewDelegate
  66. func textViewDidChange(_ textView: UITextView) {
  67. onTextFieldChange?(self.textField)
  68. }
  69. func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
  70. placeholder.isHidden = !(text.isEmpty && range.length == textView.text.count)
  71. return true
  72. }
  73. }