SDCollectionViewCell.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. }
  39. return self;
  40. }
  41. - (void)setTitleLabelBackgroundColor:(UIColor *)titleLabelBackgroundColor
  42. {
  43. _titleLabelBackgroundColor = titleLabelBackgroundColor;
  44. _titleLabel.backgroundColor = titleLabelBackgroundColor;
  45. }
  46. - (void)setTitleLabelTextColor:(UIColor *)titleLabelTextColor
  47. {
  48. _titleLabelTextColor = titleLabelTextColor;
  49. _titleLabel.textColor = titleLabelTextColor;
  50. }
  51. - (void)setTitleLabelTextFont:(UIFont *)titleLabelTextFont
  52. {
  53. _titleLabelTextFont = titleLabelTextFont;
  54. _titleLabel.font = titleLabelTextFont;
  55. }
  56. - (void)setupImageView
  57. {
  58. UIImageView *imageView = [[UIImageView alloc] init];
  59. _imageView = imageView;
  60. [self.contentView addSubview:imageView];
  61. }
  62. - (void)setupTitleLabel
  63. {
  64. UILabel *titleLabel = [[UILabel alloc] init];
  65. _titleLabel = titleLabel;
  66. _titleLabel.hidden = YES;
  67. [self.contentView addSubview:titleLabel];
  68. }
  69. - (void)setTitle:(NSString *)title
  70. {
  71. _title = [title copy];
  72. _titleLabel.text = [NSString stringWithFormat:@" %@", title];
  73. if (_titleLabel.hidden) {
  74. _titleLabel.hidden = NO;
  75. }
  76. }
  77. -(void)setTitleLabelTextAlignment:(NSTextAlignment)titleLabelTextAlignment
  78. {
  79. _titleLabelTextAlignment = titleLabelTextAlignment;
  80. _titleLabel.textAlignment = titleLabelTextAlignment;
  81. }
  82. - (void)layoutSubviews
  83. {
  84. [super layoutSubviews];
  85. if (self.onlyDisplayText) {
  86. _titleLabel.frame = self.bounds;
  87. } else {
  88. _imageView.frame = self.bounds;
  89. CGFloat titleLabelW = self.sd_width;
  90. CGFloat titleLabelH = _titleLabelHeight;
  91. CGFloat titleLabelX = 0;
  92. CGFloat titleLabelY = self.sd_height - titleLabelH;
  93. _titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
  94. }
  95. }
  96. @end