CTAssetSelectionLabel.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "CTAssetSelectionLabel.h"
  22. #import "CTAssetsPickerDefines.h"
  23. /**
  24. * The label to show selection index.
  25. */
  26. @interface CTAssetSelectionLabel ()
  27. #pragma mark Managing Auto Layout
  28. /**
  29. * The constraints of the size of the label.
  30. */
  31. @property (nonatomic, strong) NSArray *sizeConstraints;
  32. /**
  33. * The constraint for pinning the label to vertical edge.
  34. */
  35. @property (nonatomic, strong) NSLayoutConstraint *verticalConstraint;
  36. /**
  37. * The constraint for pinning the label to horizontal edge.
  38. */
  39. @property (nonatomic, strong) NSLayoutConstraint *horizontalConstraint;
  40. /**
  41. * Determines whether or not the constraints have been set up.
  42. */
  43. @property (nonatomic, assign) BOOL didSetupConstraints;
  44. @end
  45. @implementation CTAssetSelectionLabel
  46. #pragma mark Initializing a Label Object
  47. /**
  48. * Designated Initializer
  49. *
  50. * @return an initialized label object
  51. */
  52. - (instancetype)initWithFrame:(CGRect)frame
  53. {
  54. self = [super initWithFrame:frame];
  55. if (self)
  56. {
  57. self.textAlignment = NSTextAlignmentCenter;
  58. self.font = CTAssetLabelFont;
  59. self.textColor = CTAssetLabelTextColor;
  60. self.backgroundColor = CTAssetLabelBackgroundColor;
  61. self.layer.borderColor = CTAssetLabelBorderColor.CGColor;
  62. self.layer.masksToBounds = YES;
  63. self.isAccessibilityElement = NO;
  64. }
  65. return self;
  66. }
  67. #pragma mark Customizing Appearance
  68. /**
  69. * The width of the label's border
  70. */
  71. - (CGFloat)borderWidth
  72. {
  73. return self.layer.borderWidth;
  74. }
  75. - (void)setBorderWidth:(CGFloat)borderWidth
  76. {
  77. self.layer.borderWidth = borderWidth;
  78. }
  79. /**
  80. * The color of the label's border
  81. */
  82. - (UIColor *)borderColor
  83. {
  84. return [UIColor colorWithCGColor:self.layer.borderColor];
  85. }
  86. - (void)setBorderColor:(UIColor *)borderColor
  87. {
  88. UIColor *color = (borderColor) ? borderColor : CTAssetLabelBorderColor;
  89. self.layer.borderColor = color.CGColor;
  90. }
  91. /**
  92. * To set the size of label.
  93. *
  94. * @param size The size of the label.
  95. */
  96. - (void)setSize:(CGSize)size
  97. {
  98. if (CGSizeEqualToSize(size, CGSizeZero)) {
  99. size = CTAssetLabelSize;
  100. }
  101. [self removeConstraints:self.sizeConstraints];
  102. [NSLayoutConstraint autoSetPriority:UILayoutPriorityRequired forConstraints:^{
  103. self.sizeConstraints = [self autoSetDimensionsToSize:size];
  104. }];
  105. }
  106. /**
  107. * To set the size of label.
  108. *
  109. * @param cornerRadius The radius to use when drawing rounded corners for the label’s background.
  110. */
  111. - (void)setCornerRadius:(CGFloat)cornerRadius
  112. {
  113. self.layer.cornerRadius = cornerRadius;
  114. }
  115. /**
  116. * To set margin of the label from the edges.
  117. *
  118. * @param margin The margin from the edges.
  119. *
  120. * @see setMargin:forVerticalEdge:horizontalEdge:
  121. */
  122. - (void)setMargin:(CGFloat)margin
  123. {
  124. [self setMargin:margin forVerticalEdge:NSLayoutAttributeRight horizontalEdge:NSLayoutAttributeBottom];
  125. }
  126. /**
  127. * To set margin of the label from specific edges.
  128. *
  129. * @param margin The margin from the edges.
  130. * @param edgeX The layout attribute respresents vertical edge that the label pins to. Either `NSLayoutAttributeLeft` or `NSLayoutAttributeRight`.
  131. * @param edgeY The layout attribute respresents horizontal edge that the label pins to. Either `NSLayoutAttributeTop` or `NSLayoutAttributeBottom`.
  132. *
  133. * @see setMargin:
  134. */
  135. - (void)setMargin:(CGFloat)margin forVerticalEdge:(NSLayoutAttribute)edgeX horizontalEdge:(NSLayoutAttribute)edgeY
  136. {
  137. NSAssert(edgeX == NSLayoutAttributeLeft || edgeX == NSLayoutAttributeRight,
  138. @"Vertical edge must be NSLayoutAttributeLeft or NSLayoutAttributeRight");
  139. NSAssert(edgeY == NSLayoutAttributeTop || edgeY == NSLayoutAttributeBottom,
  140. @"Horizontal edge must be NSLayoutAttributeTop or NSLayoutAttributeBottom");
  141. [self.superview removeConstraints:@[self.verticalConstraint, self.horizontalConstraint]];
  142. self.verticalConstraint = [self autoPinEdgeToSuperviewEdge:(ALEdge)edgeX withInset:margin];
  143. self.horizontalConstraint = [self autoPinEdgeToSuperviewEdge:(ALEdge)edgeY withInset:margin];
  144. }
  145. /**
  146. * To set the text attributes to display the label.
  147. *
  148. * Currently only supports attributes `NSFontAttributeName`, `NSForegroundColorAttributeName` and `NSBackgroundColorAttributeName`.
  149. *
  150. * @param textAttributes The text attributes used to display the label.
  151. */
  152. - (void)setTextAttributes:(NSDictionary *)textAttributes
  153. {
  154. self.font = (textAttributes) ? textAttributes[NSFontAttributeName] : CTAssetLabelFont;
  155. self.textColor = (textAttributes) ? textAttributes[NSForegroundColorAttributeName] : CTAssetLabelTextColor;
  156. self.backgroundColor = (textAttributes) ? textAttributes[NSBackgroundColorAttributeName] : CTAssetLabelBackgroundColor;
  157. }
  158. #pragma mark Triggering Auto Layout
  159. /**
  160. * Updates constraints of the label.
  161. */
  162. - (void)updateConstraints
  163. {
  164. if (!self.didSetupConstraints)
  165. {
  166. [NSLayoutConstraint autoSetPriority:UILayoutPriorityRequired forConstraints:^{
  167. self.sizeConstraints = [self autoSetDimensionsToSize:CTAssetLabelSize];
  168. }];
  169. self.verticalConstraint = [self autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
  170. self.horizontalConstraint = [self autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
  171. self.didSetupConstraints = YES;
  172. }
  173. [super updateConstraints];
  174. }
  175. @end