优惠券swift版马甲包

ZCycleViewCell.swift 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // ZCycleViewCell.swift
  3. // ZCycleView
  4. //
  5. // Created by mengqingzheng on 2017/11/17.
  6. // Copyright © 2017年 MQZHot. All rights reserved.
  7. //
  8. import UIKit
  9. import Kingfisher
  10. enum ResourceType {
  11. case image
  12. case imageURL
  13. case text
  14. }
  15. class ZCycleViewCell: UICollectionViewCell {
  16. var titleContainerViewH: CGFloat = 25
  17. /// set text
  18. func attributeString(_ attributeString: NSAttributedString?, titleImgURL: String? = nil, titleImage: UIImage? = nil, titleImageSize: CGSize? = nil) {
  19. titleLabel.attributedText = attributeString
  20. titleContainerView.frame = CGRect(x: 0, y: contentView.bounds.size.height-titleContainerViewH, width: contentView.bounds.size.width, height:titleContainerViewH)
  21. titleContainerView.isHidden = attributeString == nil || attributeString?.string == "" ? true : false
  22. let containerViewSize = titleContainerView.bounds.size
  23. if let imageSize = titleImageSize {
  24. titleImageView.frame = CGRect(x: 5, y: (containerViewSize.height-imageSize.height)/2, width: imageSize.width, height: imageSize.height)
  25. titleLabel.frame = CGRect(x: 6+imageSize.width, y: 0, width: containerViewSize.width-6-imageSize.width, height: containerViewSize.height)
  26. } else {
  27. titleImageView.frame = CGRect.zero
  28. titleLabel.frame = CGRect(x: 5, y: 0, width: containerViewSize.width-5, height: containerViewSize.height)
  29. }
  30. if titleImgURL != nil {
  31. if let url = URL(string: titleImgURL!) {
  32. titleImageView.kf.setImage(with: url)
  33. }
  34. } else {
  35. titleImageView.image = titleImage
  36. }
  37. }
  38. /// set image
  39. func imageUrl(_ imageUrl: String?, placeholder: UIImage?) {
  40. guard let imageUrl = imageUrl,
  41. let url = URL(string: imageUrl) else {
  42. imageView.image = placeholder
  43. return
  44. }
  45. imageView.kf.setImage(with: url, placeholder: placeholder, options: [.transition(.fade(0.3))])
  46. }
  47. var imageView: UIImageView!
  48. var titleLabel: UILabel!
  49. var titleContainerView: UIView!
  50. var titleImageView: UIImageView!
  51. override init(frame: CGRect) {
  52. super.init(frame: frame)
  53. addImageView()
  54. addTitleLabel()
  55. }
  56. required init?(coder aDecoder: NSCoder) {
  57. fatalError("init(coder:) has not been implemented")
  58. }
  59. func addImageView() {
  60. imageView = UIImageView(frame: contentView.bounds)
  61. imageView.clipsToBounds = true
  62. contentView.addSubview(imageView)
  63. }
  64. func addTitleLabel() {
  65. titleContainerView = UIView(frame: CGRect(x: 0, y: contentView.bounds.size.height-25, width: contentView.bounds.size.width, height: 25))
  66. titleContainerView.isHidden = true
  67. contentView.addSubview(titleContainerView)
  68. titleImageView = UIImageView()
  69. titleContainerView.addSubview(titleImageView)
  70. titleLabel = UILabel()
  71. titleLabel.clipsToBounds = true
  72. titleContainerView.addSubview(titleLabel)
  73. }
  74. }