口袋优选

SDCollectionViewCell.m 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.layer.cornerRadius = 5;
  37. self.layer.masksToBounds = YES;
  38. self.clipsToBounds = YES;
  39. [self setupImageView];
  40. [self setupTitleLabel];
  41. [self setUpSubjectLabel];
  42. }
  43. return self;
  44. }
  45. - (void)setTitleLabelBackgroundColor:(UIColor *)titleLabelBackgroundColor
  46. {
  47. _titleLabelBackgroundColor = titleLabelBackgroundColor;
  48. _titleLabel.backgroundColor = titleLabelBackgroundColor;
  49. }
  50. - (void)setTitleLabelTextColor:(UIColor *)titleLabelTextColor
  51. {
  52. _titleLabelTextColor = titleLabelTextColor;
  53. _titleLabel.textColor = titleLabelTextColor;
  54. }
  55. - (void)setTitleLabelTextFont:(UIFont *)titleLabelTextFont
  56. {
  57. _titleLabelTextFont = titleLabelTextFont;
  58. _titleLabel.font = titleLabelTextFont;
  59. }
  60. - (void)setupImageView
  61. {
  62. UIImageView *imageView = [[UIImageView alloc] init];
  63. imageView.backgroundColor = [UIColor yhGrayColor];
  64. _imageView = imageView;
  65. _imageView.layer.cornerRadius = 5;
  66. _imageView.layer.masksToBounds = YES;
  67. _imageView.clipsToBounds = YES;
  68. [self.contentView addSubview:imageView];
  69. }
  70. - (void)setupTitleLabel
  71. {
  72. UILabel *titleLabel = [[UILabel alloc] init];
  73. _titleLabel = titleLabel;
  74. _titleLabel.hidden = YES;
  75. [self.contentView addSubview:titleLabel];
  76. }
  77. - (void)setUpSubjectLabel {
  78. _subjectLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
  79. _subjectLb.backgroundColor = [UIColor redColor];
  80. _subjectLb.text = @"专题";
  81. _subjectLb.font = [UIFont systemFontOfSize:14];
  82. _subjectLb.textAlignment = NSTextAlignmentCenter;
  83. _subjectLb.textColor = [UIColor whiteColor];
  84. [self.imageView addSubview:_subjectLb];
  85. _subjectLb.hidden = YES;
  86. }
  87. - (void)setTitle:(NSString *)title
  88. {
  89. _title = [title copy];
  90. _titleLabel.text = [NSString stringWithFormat:@" %@", title];
  91. if (_titleLabel.hidden) {
  92. _titleLabel.hidden = NO;
  93. }
  94. }
  95. - (void)layoutSubviews
  96. {
  97. [super layoutSubviews];
  98. if (self.onlyDisplayText) {
  99. _titleLabel.frame = self.bounds;
  100. } else {
  101. _imageView.frame = self.bounds;
  102. CGFloat titleLabelW = self.sd_width;
  103. CGFloat titleLabelH = _titleLabelHeight;
  104. CGFloat titleLabelX = 0;
  105. CGFloat titleLabelY = self.sd_height - titleLabelH;
  106. _titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
  107. }
  108. }
  109. @end