省钱达人

SDCollectionViewCell.m 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // SDCollectionViewCell.m
  3. // SDCycleScrollView
  4. //
  5. // Created by aier on 15-3-22.
  6. // Copyright (c) 2015年 GSD. All rights reserved.
  7. //
  8. /*
  9. *********************************************************************************
  10. *
  11. * 🌟🌟🌟 新建SDCycleScrollView交流QQ群:185534916 🌟🌟🌟
  12. *
  13. * 在您使用此自动轮播库的过程中如果出现bug请及时以以下任意一种方式联系我们,我们会及时修复bug并
  14. * 帮您解决问题。
  15. * 新浪微博:GSD_iOS
  16. * Email : gsdios@126.com
  17. * GitHub: https://github.com/gsdios
  18. *
  19. * 另(我的自动布局库SDAutoLayout):
  20. * 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于
  21. * 做最简单易用的AutoLayout库。
  22. * 视频教程:http://www.letv.com/ptv/vplay/24038772.html
  23. * 用法示例:https://github.com/gsdios/SDAutoLayout/blob/master/README.md
  24. * GitHub:https://github.com/gsdios/SDAutoLayout
  25. *********************************************************************************
  26. */
  27. #import "SDCollectionViewCell.h"
  28. #import "UIView+SDExtension.h"
  29. @implementation SDCollectionViewCell
  30. {
  31. __weak UILabel *_titleLabel;
  32. }
  33. - (instancetype)initWithFrame:(CGRect)frame
  34. {
  35. if (self = [super initWithFrame:frame]) {
  36. [self setupImageView];
  37. [self setupTitleLabel];
  38. [self setUpSubjectLabel];
  39. }
  40. return self;
  41. }
  42. - (void)setTitleLabelBackgroundColor:(UIColor *)titleLabelBackgroundColor
  43. {
  44. _titleLabelBackgroundColor = titleLabelBackgroundColor;
  45. _titleLabel.backgroundColor = titleLabelBackgroundColor;
  46. }
  47. - (void)setTitleLabelTextColor:(UIColor *)titleLabelTextColor
  48. {
  49. _titleLabelTextColor = titleLabelTextColor;
  50. _titleLabel.textColor = titleLabelTextColor;
  51. }
  52. - (void)setTitleLabelTextFont:(UIFont *)titleLabelTextFont
  53. {
  54. _titleLabelTextFont = titleLabelTextFont;
  55. _titleLabel.font = titleLabelTextFont;
  56. }
  57. - (void)setupImageView
  58. {
  59. UIImageView *imageView = [[UIImageView alloc] init];
  60. imageView.backgroundColor = [UIColor yhGrayColor];
  61. _imageView = imageView;
  62. [self.contentView addSubview:imageView];
  63. }
  64. - (void)setupTitleLabel
  65. {
  66. UILabel *titleLabel = [[UILabel alloc] init];
  67. _titleLabel = titleLabel;
  68. _titleLabel.hidden = YES;
  69. [self.contentView addSubview:titleLabel];
  70. }
  71. - (void)setUpSubjectLabel {
  72. _subjectLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
  73. _subjectLb.backgroundColor = [UIColor redColor];
  74. _subjectLb.text = @"专题";
  75. _subjectLb.font = [UIFont systemFontOfSize:14];
  76. _subjectLb.textAlignment = NSTextAlignmentCenter;
  77. _subjectLb.textColor = [UIColor whiteColor];
  78. [self.imageView addSubview:_subjectLb];
  79. _subjectLb.hidden = YES;
  80. }
  81. - (void)setTitle:(NSString *)title
  82. {
  83. _title = [title copy];
  84. _titleLabel.text = [NSString stringWithFormat:@" %@", title];
  85. if (_titleLabel.hidden) {
  86. _titleLabel.hidden = NO;
  87. }
  88. }
  89. - (void)layoutSubviews
  90. {
  91. [super layoutSubviews];
  92. if (self.onlyDisplayText) {
  93. _titleLabel.frame = self.bounds;
  94. } else {
  95. _imageView.frame = self.bounds;
  96. CGFloat titleLabelW = self.sd_width;
  97. CGFloat titleLabelH = _titleLabelHeight;
  98. CGFloat titleLabelX = 0;
  99. CGFloat titleLabelY = self.sd_height - titleLabelH;
  100. _titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
  101. }
  102. }
  103. @end