CPDFThumbnailViewCell.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // CPDFThumbnailViewCell.swift
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. class CPDFThumbnailViewCell: UICollectionViewCell {
  14. var textLabel: UILabel?
  15. var imageView: UIImageView?
  16. var imageSize: CGSize = .zero
  17. override init(frame: CGRect) {
  18. super.init(frame: frame)
  19. imageView = UIImageView()
  20. imageView?.layer.borderWidth = 1.0
  21. imageView?.layer.borderColor = UIColor(red: 221/255.0, green: 233/255.0, blue: 255/255.0, alpha: 1.0).cgColor
  22. if imageView != nil {
  23. contentView.addSubview(imageView!)
  24. }
  25. textLabel = UILabel(frame: CGRect(x: 0, y: frame.size.height - 12, width: frame.size.width, height: 12))
  26. textLabel?.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
  27. textLabel?.textAlignment = .center
  28. textLabel?.font = UIFont.systemFont(ofSize: 13)
  29. textLabel?.textColor = UIColor.black
  30. if textLabel != nil {
  31. contentView.addSubview(textLabel!)
  32. }
  33. }
  34. required init?(coder aDecoder: NSCoder) {
  35. fatalError("init(coder:) has not been implemented")
  36. }
  37. override func layoutSubviews() {
  38. imageView?.frame = CGRect(x: (frame.size.width - imageSize.width)/2, y: (frame.size.height - 14 - imageSize.height) / 2, width: imageSize.width, height: imageSize.height)
  39. let startW = textLabel?.text?.size(withAttributes: [.font: UIFont.systemFont(ofSize: 10)]).width ?? 0
  40. textLabel?.frame = CGRect(x: frame.size.width/2 - (startW + 20)/2, y: imageSize.height + (frame.size.height - 14 - imageSize.height) / 2, width: startW + 20, height: 12)
  41. }
  42. func setPageRef(_ pageRef: CGPDFPage?) {
  43. var boxRect = CGRect.zero
  44. if let pageRef = pageRef {
  45. boxRect = pageRef.getBoxRect(.cropBox)
  46. let displayBounds = CGRect(x: 0, y: 0, width: bounds.size.width, height: bounds.size.height - 12)
  47. let transform = pageRef.getDrawingTransform(.cropBox, rect: displayBounds, rotate: 0, preserveAspectRatio: true)
  48. boxRect = boxRect.applying(transform)
  49. }
  50. }
  51. override var isSelected: Bool {
  52. didSet {
  53. super.isSelected = isSelected
  54. if isSelected {
  55. textLabel?.backgroundColor = UIColor(red: 20/255, green: 96/255, blue: 243/255, alpha: 1)
  56. textLabel?.textColor = UIColor.white
  57. imageView?.layer.borderColor = UIColor(red: 20/255, green: 96/255, blue: 243/255, alpha: 1).cgColor
  58. imageView?.layer.borderWidth = 2
  59. imageView?.layer.cornerRadius = 4
  60. imageView?.clipsToBounds = true
  61. } else {
  62. textLabel?.backgroundColor = UIColor.clear
  63. textLabel?.textColor = UIColor.black
  64. imageView?.layer.borderColor = UIColor(red: 242/255, green: 242/255, blue: 242/255, alpha: 1).cgColor
  65. imageView?.layer.borderWidth = 1
  66. }
  67. }
  68. }
  69. }