AutocompleteCell.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // AutocompleteCell.swift
  3. // InputBarAccessoryView
  4. //
  5. // Copyright © 2017-2019 Nathan Tannar.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24. //
  25. // Created by Nathan Tannar on 10/4/17.
  26. //
  27. import UIKit
  28. open class AutocompleteCell: UITableViewCell {
  29. // MARK: - Properties
  30. open class var reuseIdentifier: String {
  31. return "AutocompleteCell"
  32. }
  33. /// A boarder line anchored to the top of the view
  34. public let separatorLine = SeparatorLine()
  35. open var imageViewEdgeInsets: UIEdgeInsets = .zero { didSet { setNeedsLayout() } }
  36. // MARK: - Initialization
  37. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  38. super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
  39. setup()
  40. }
  41. required public init?(coder aDecoder: NSCoder) {
  42. super.init(coder: aDecoder)
  43. setup()
  44. }
  45. open override func prepareForReuse() {
  46. super.prepareForReuse()
  47. textLabel?.text = nil
  48. detailTextLabel?.text = nil
  49. imageView?.image = nil
  50. imageViewEdgeInsets = .zero
  51. separatorLine.backgroundColor = .lightGray
  52. separatorLine.isHidden = false
  53. }
  54. // MARK: - Setup
  55. private func setup() {
  56. setupSubviews()
  57. setupConstraints()
  58. }
  59. open func setupSubviews() {
  60. addSubview(separatorLine)
  61. }
  62. open func setupConstraints() {
  63. separatorLine.addConstraints(left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, heightConstant: 0.5)
  64. }
  65. open override func layoutSubviews() {
  66. super.layoutSubviews()
  67. guard let imageViewFrame = imageView?.frame else { return }
  68. let imageViewOrigin = CGPoint(x: imageViewFrame.origin.x + imageViewEdgeInsets.left, y: imageViewFrame.origin.y + imageViewEdgeInsets.top)
  69. let imageViewSize = CGSize(width: imageViewFrame.size.width - imageViewEdgeInsets.left - imageViewEdgeInsets.right, height: imageViewFrame.size.height - imageViewEdgeInsets.top - imageViewEdgeInsets.bottom)
  70. imageView?.frame = CGRect(origin: imageViewOrigin, size: imageViewSize)
  71. }
  72. // MARK: - API [Public]
  73. @available(*, deprecated, message: "This function has been moved to the `AutocompleteManager`")
  74. open func attributedText(matching session: AutocompleteSession) -> NSMutableAttributedString {
  75. fatalError("Please use `func attributedText(matching:, fontSize:)` implemented in the `AutocompleteManager`")
  76. }
  77. }