CTAssetCollectionViewCell.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. MIT License (MIT)
  3. Copyright (c) 2015 Clement CN Tsang
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #import <PureLayout/PureLayout.h>
  21. #import "CTAssetsPickerDefines.h"
  22. #import "CTAssetCollectionViewCell.h"
  23. #import "NSBundle+CTAssetsPickerController.h"
  24. #import "UIImage+CTAssetsPickerController.h"
  25. #import "NSNumberFormatter+CTAssetsPickerController.h"
  26. @interface CTAssetCollectionViewCell ()
  27. @property (nonatomic, assign) CGSize thumbnailSize;
  28. @property (nonatomic, strong) CTAssetThumbnailStacks *thumbnailStacks;
  29. @property (nonatomic, strong) UIView *labelsView;
  30. @property (nonatomic, strong) UILabel *titleLabel;
  31. @property (nonatomic, strong) UILabel *countLabel;
  32. @property (nonatomic, assign) BOOL didSetupConstraints;
  33. @property (nonatomic, strong) PHAssetCollection *collection;
  34. @property (nonatomic, assign) NSUInteger count;
  35. @end
  36. @implementation CTAssetCollectionViewCell
  37. - (instancetype)initWithThumbnailSize:(CGSize)size reuseIdentifier:(NSString *)reuseIdentifier;
  38. {
  39. if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])
  40. {
  41. _thumbnailSize = size;
  42. _titleTextColor = CTAssetCollectionViewCellTitleTextColor;
  43. _selectedTitleTextColor = CTAssetCollectionViewCellTitleTextColor;
  44. _countTextColor = CTAssetCollectionViewCellCountTextColor;
  45. _selectedCountTextColor = CTAssetCollectionViewCellCountTextColor;
  46. _accessoryColor = CTAssetCollectionViewCellAccessoryColor;
  47. _selectedAccessoryColor = CTAssetCollectionViewCellAccessoryColor;
  48. self.opaque = YES;
  49. self.isAccessibilityElement = YES;
  50. self.textLabel.backgroundColor = self.backgroundColor;
  51. self.detailTextLabel.backgroundColor = self.backgroundColor;
  52. self.accessoryType = UITableViewCellAccessoryNone;
  53. [self setupViews];
  54. }
  55. return self;
  56. }
  57. #pragma mark - Setup
  58. - (void)setupViews
  59. {
  60. CTAssetThumbnailStacks *thumbnailStacks = [CTAssetThumbnailStacks newAutoLayoutView];
  61. thumbnailStacks.thumbnailSize = self.thumbnailSize;
  62. self.thumbnailStacks = thumbnailStacks;
  63. UILabel *titleLabel = [UILabel newAutoLayoutView];
  64. titleLabel.font = CTAssetCollectionViewCellTitleFont;
  65. titleLabel.textColor = self.titleTextColor;
  66. self.titleLabel = titleLabel;
  67. UILabel *countLabel = [UILabel newAutoLayoutView];
  68. countLabel.font = CTAssetCollectionViewCellCountFont;
  69. countLabel.textColor = self.countTextColor;
  70. self.countLabel = countLabel;
  71. UIView *labelsView = [UIView newAutoLayoutView];
  72. [labelsView addSubview:self.titleLabel];
  73. [labelsView addSubview:self.countLabel];
  74. self.labelsView = labelsView;
  75. [self.contentView addSubview:self.thumbnailStacks];
  76. [self.contentView addSubview:self.labelsView];
  77. UIImage *accessory = [UIImage ctassetsPickerImageNamed:@"DisclosureArrow"];
  78. accessory = [accessory imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  79. UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessory];
  80. accessoryView.tintColor = self.accessoryColor;
  81. self.accessoryView = accessoryView;
  82. }
  83. - (void)setupPlaceholderImage
  84. {
  85. NSString *imageName = [self placeHolderImageNameOfCollectionSubtype:self.collection.assetCollectionSubtype];
  86. UIImage *image = [UIImage ctassetsPickerImageNamed:imageName];
  87. image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  88. for (CTAssetThumbnailView *thumbnailView in self.thumbnailStacks.thumbnailViews)
  89. {
  90. [thumbnailView bind:nil assetCollection:nil];
  91. thumbnailView.backgroundImage = image;
  92. }
  93. }
  94. - (NSString *)placeHolderImageNameOfCollectionSubtype:(PHAssetCollectionSubtype)subtype
  95. {
  96. if (subtype == PHAssetCollectionSubtypeSmartAlbumUserLibrary)
  97. return @"GridEmptyCameraRoll";
  98. else if (subtype == PHAssetCollectionSubtypeSmartAlbumAllHidden)
  99. return @"GridHiddenAlbum";
  100. else if (subtype == PHAssetCollectionSubtypeAlbumCloudShared)
  101. return @"GridEmptyAlbumShared";
  102. else
  103. return @"GridEmptyAlbum";
  104. }
  105. #pragma mark - Apperance
  106. - (UIFont *)titleFont
  107. {
  108. return self.titleLabel.font;
  109. }
  110. - (void)setTitleFont:(UIFont *)titleFont
  111. {
  112. UIFont *font = (titleFont) ? titleFont : CTAssetCollectionViewCellTitleFont;
  113. self.titleLabel.font = font;
  114. }
  115. - (void)setTitleTextColor:(UIColor *)titleTextColor
  116. {
  117. UIColor *color = (titleTextColor) ? titleTextColor : CTAssetCollectionViewCellTitleTextColor;
  118. _titleTextColor = color;
  119. }
  120. - (void)setSelectedTitleTextColor:(UIColor *)titleTextColor
  121. {
  122. UIColor *color = (titleTextColor) ? titleTextColor : CTAssetCollectionViewCellTitleTextColor;
  123. _selectedTitleTextColor = color;
  124. }
  125. - (UIFont *)countFont
  126. {
  127. return self.countLabel.font;
  128. }
  129. - (void)setCountFont:(UIFont *)countFont
  130. {
  131. UIFont *font = (countFont) ? countFont : CTAssetCollectionViewCellCountFont;
  132. self.countLabel.font = font;
  133. }
  134. - (void)setCountTextColor:(UIColor *)countTextColor
  135. {
  136. UIColor *color = (countTextColor) ? countTextColor : CTAssetCollectionViewCellCountTextColor;
  137. _countTextColor = color;
  138. }
  139. - (void)setSelectedCountTextColor:(UIColor *)countTextColor
  140. {
  141. UIColor *color = (countTextColor) ? countTextColor : CTAssetCollectionViewCellCountTextColor;
  142. _selectedCountTextColor = color;
  143. }
  144. - (void)setAccessoryColor:(UIColor *)accessoryColor
  145. {
  146. UIColor *color = (accessoryColor) ? accessoryColor : CTAssetCollectionViewCellAccessoryColor;
  147. _accessoryColor = color;
  148. }
  149. - (void)setSelectedAccessoryColor:(UIColor *)accessoryColor
  150. {
  151. UIColor *color = (accessoryColor) ? accessoryColor : CTAssetCollectionViewCellAccessoryColor;
  152. _selectedAccessoryColor = color;
  153. }
  154. - (UIColor *)selectedBackgroundColor
  155. {
  156. return self.selectedBackgroundView.backgroundColor;
  157. }
  158. - (void)setSelectedBackgroundColor:(UIColor *)selectedBackgroundColor
  159. {
  160. if (!selectedBackgroundColor)
  161. self.selectedBackgroundView = nil;
  162. else
  163. {
  164. UIView *view = [UIView new];
  165. view.backgroundColor = selectedBackgroundColor;
  166. self.selectedBackgroundView = view;
  167. }
  168. }
  169. #pragma mark - Override highlighted / selected
  170. - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
  171. {
  172. [super setHighlighted:highlighted animated:animated];
  173. [self.thumbnailStacks setHighlighted:highlighted];
  174. self.titleLabel.textColor = (highlighted) ? self.selectedTitleTextColor : self.titleTextColor;
  175. self.countLabel.textColor = (highlighted) ? self.selectedCountTextColor : self.countTextColor;
  176. self.accessoryView.tintColor = (highlighted) ? self.selectedAccessoryColor : self.accessoryColor;
  177. }
  178. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  179. {
  180. [super setSelected:selected animated:animated];
  181. [self.thumbnailStacks setHighlighted:selected];
  182. self.titleLabel.textColor = (selected) ? self.selectedTitleTextColor : self.titleTextColor;
  183. self.countLabel.textColor = (selected) ? self.selectedCountTextColor : self.countTextColor;
  184. self.accessoryView.tintColor = (selected) ? self.selectedAccessoryColor : self.accessoryColor;
  185. }
  186. #pragma mark - Update auto layout constraints
  187. - (void)updateConstraints
  188. {
  189. if (!self.didSetupConstraints)
  190. {
  191. CGSize size = self.thumbnailSize;
  192. CGFloat top = self.thumbnailStacks.edgeInsets.top;
  193. size.height += top;
  194. [NSLayoutConstraint autoSetPriority:UILayoutPriorityRequired forConstraints:^{
  195. [self.thumbnailStacks autoSetDimensionsToSize:size];
  196. }];
  197. [NSLayoutConstraint autoSetPriority:UILayoutPriorityDefaultHigh forConstraints:^{
  198. [self.thumbnailStacks autoPinEdgesToSuperviewMarginsExcludingEdge:ALEdgeTrailing];
  199. }];
  200. [self.labelsView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
  201. [self.labelsView autoPinEdge:ALEdgeLeading
  202. toEdge:ALEdgeTrailing
  203. ofView:self.thumbnailStacks
  204. withOffset:self.labelsView.layoutMargins.left
  205. relation:NSLayoutRelationGreaterThanOrEqual];
  206. [self.titleLabel autoPinEdgesToSuperviewMarginsExcludingEdge:ALEdgeBottom];
  207. [self.countLabel autoPinEdgesToSuperviewMarginsExcludingEdge:ALEdgeTop];
  208. [self.countLabel autoPinEdge:ALEdgeTop
  209. toEdge:ALEdgeBottom
  210. ofView:self.titleLabel
  211. withOffset:self.countLabel.layoutMargins.top
  212. relation:NSLayoutRelationGreaterThanOrEqual];
  213. self.didSetupConstraints = YES;
  214. }
  215. [super updateConstraints];
  216. }
  217. #pragma mark - Bind asset collection
  218. - (void)bind:(PHAssetCollection *)collection count:(NSUInteger)count
  219. {
  220. self.collection = collection;
  221. self.count = count;
  222. [self setupPlaceholderImage];
  223. self.titleLabel.text = collection.localizedTitle;
  224. if (count != NSNotFound)
  225. {
  226. NSNumberFormatter *nf = [NSNumberFormatter new];
  227. self.countLabel.text = [nf ctassetsPickerStringFromAssetsCount:count];
  228. }
  229. [self setNeedsUpdateConstraints];
  230. [self updateConstraintsIfNeeded];
  231. }
  232. #pragma mark - Accessibility label
  233. - (NSString *)accessibilityLabel
  234. {
  235. NSString *title = self.titleLabel.text;
  236. NSString *count = [NSString stringWithFormat:CTAssetsPickerLocalizedString(@"%@ Photos", nil), self.countLabel.text];
  237. NSArray *labels = @[title, count];
  238. return [labels componentsJoinedByString:@","];
  239. }
  240. @end