暂无描述

LGAlertViewCell.m 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // LGAlertViewCell.m
  3. // LGAlertView
  4. //
  5. //
  6. // The MIT License (MIT)
  7. //
  8. // Copyright © 2015 Grigory Lutkov <Friend.LGA@gmail.com>
  9. // (https://github.com/Friend-LGA/LGAlertView)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in all
  19. // copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. // SOFTWARE.
  28. //
  29. #import "LGAlertViewCell.h"
  30. #import "LGAlertViewHelper.h"
  31. @interface LGAlertViewCell ()
  32. @property (readwrite) UIView *separatorView;
  33. @end
  34. @implementation LGAlertViewCell
  35. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  36. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  37. if (self) {
  38. self.selectionStyle = UITableViewCellSelectionStyleNone;
  39. [self clearBackgrounds];
  40. self.separatorView = [UIView new];
  41. [self addSubview:self.separatorView];
  42. self.enabled = YES;
  43. }
  44. return self;
  45. }
  46. - (void)layoutSubviews {
  47. [super layoutSubviews];
  48. CGFloat frameWidth = CGRectGetWidth(self.bounds);
  49. CGFloat frameHeight = CGRectGetHeight(self.bounds);
  50. // -----
  51. BOOL imageExists = (self.imageView.image != nil);
  52. CGSize imageSize = self.imageView.image.size;
  53. CGFloat textLabelMaxWidth;
  54. if (imageExists) {
  55. textLabelMaxWidth = frameWidth - (LGAlertViewPaddingWidth * 2.0) - imageSize.width - LGAlertViewButtonImageOffsetFromTitle;
  56. }
  57. else {
  58. textLabelMaxWidth = frameWidth - (LGAlertViewPaddingWidth * 2.0);
  59. }
  60. CGRect textLabelFrame = CGRectMake(NSNotFound,
  61. LGAlertViewPaddingHeight,
  62. textLabelMaxWidth,
  63. frameHeight - (LGAlertViewPaddingHeight * 2.0));
  64. if (self.textLabel.textAlignment == NSTextAlignmentLeft) {
  65. textLabelFrame.origin.x = LGAlertViewPaddingWidth;
  66. if (imageExists && self.iconPosition == LGAlertViewButtonIconPositionLeft) {
  67. textLabelFrame.origin.x += imageSize.width + LGAlertViewButtonImageOffsetFromTitle;
  68. }
  69. }
  70. else if (self.textLabel.textAlignment == NSTextAlignmentRight) {
  71. textLabelFrame.origin.x = frameWidth - textLabelMaxWidth - LGAlertViewPaddingWidth;
  72. if (imageExists && self.iconPosition == LGAlertViewButtonIconPositionRight) {
  73. textLabelFrame.origin.x -= imageSize.width + LGAlertViewButtonImageOffsetFromTitle;
  74. }
  75. }
  76. else {
  77. textLabelFrame.origin.x = (frameWidth / 2.0) - (textLabelMaxWidth / 2.0);
  78. if (imageExists) {
  79. if (self.iconPosition == LGAlertViewButtonIconPositionLeft) {
  80. textLabelFrame.origin.x += (imageSize.width / 2.0) + (LGAlertViewButtonImageOffsetFromTitle / 2.0);
  81. }
  82. else {
  83. textLabelFrame.origin.x -= (imageSize.width / 2.0) + (LGAlertViewButtonImageOffsetFromTitle / 2.0);
  84. }
  85. }
  86. }
  87. if (LGAlertViewHelper.isNotRetina) {
  88. textLabelFrame = CGRectIntegral(textLabelFrame);
  89. }
  90. self.textLabel.frame = textLabelFrame;
  91. // -----
  92. if (self.imageView.image) {
  93. CGSize imageSize = self.imageView.image.size;
  94. CGRect imageViewFrame = CGRectMake(NSNotFound,
  95. (frameHeight / 2.0) - (imageSize.height / 2.0),
  96. imageSize.width,
  97. imageSize.height);
  98. if (self.textLabel.textAlignment == NSTextAlignmentCenter) {
  99. CGSize textLabelSize = [self.textLabel sizeThatFits:CGSizeMake(textLabelMaxWidth, CGFLOAT_MAX)];
  100. CGFloat imageAndTextWidth = imageSize.width + textLabelSize.width + LGAlertViewButtonImageOffsetFromTitle;
  101. if (self.iconPosition == LGAlertViewButtonIconPositionLeft) {
  102. imageViewFrame.origin.x = (frameWidth / 2.0) - (imageAndTextWidth / 2.0);
  103. }
  104. else {
  105. imageViewFrame.origin.x = (frameWidth / 2.0) + (imageAndTextWidth / 2.0) - imageSize.width;
  106. }
  107. }
  108. else {
  109. if (self.iconPosition == LGAlertViewButtonIconPositionLeft) {
  110. imageViewFrame.origin.x = LGAlertViewPaddingWidth;
  111. }
  112. else {
  113. imageViewFrame.origin.x = frameWidth - imageSize.width - LGAlertViewPaddingWidth;
  114. }
  115. }
  116. if (LGAlertViewHelper.isNotRetina) {
  117. imageViewFrame = CGRectIntegral(imageViewFrame);
  118. }
  119. self.imageView.frame = imageViewFrame;
  120. }
  121. // -----
  122. if (!self.separatorView.hidden) {
  123. self.separatorView.frame = CGRectMake(0.0,
  124. CGRectGetHeight(self.bounds) - LGAlertViewHelper.separatorHeight,
  125. CGRectGetWidth(self.bounds),
  126. LGAlertViewHelper.separatorHeight);
  127. }
  128. }
  129. - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
  130. [super setHighlighted:highlighted animated:animated];
  131. if (highlighted) {
  132. self.textLabel.textColor = self.titleColorHighlighted;
  133. self.imageView.image = self.imageHighlighted;
  134. self.backgroundColor = self.backgroundColorHighlighted;
  135. }
  136. else {
  137. [self setEnabled:self.enabled];
  138. }
  139. [self clearBackgrounds];
  140. }
  141. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  142. [super setSelected:selected animated:animated];
  143. if (selected) {
  144. self.textLabel.textColor = self.titleColorHighlighted;
  145. self.imageView.image = self.imageHighlighted;
  146. self.backgroundColor = self.backgroundColorHighlighted;
  147. }
  148. else {
  149. [self setEnabled:self.enabled];
  150. }
  151. [self clearBackgrounds];
  152. }
  153. - (void)setEnabled:(BOOL)enabled {
  154. _enabled = enabled;
  155. self.userInteractionEnabled = enabled;
  156. if (enabled) {
  157. self.textLabel.textColor = self.titleColor;
  158. self.imageView.image = self.image;
  159. self.backgroundColor = self.backgroundColorNormal;
  160. }
  161. else {
  162. self.textLabel.textColor = self.titleColorDisabled;
  163. self.imageView.image = self.imageDisabled;
  164. self.backgroundColor = self.backgroundColorDisabled;
  165. }
  166. }
  167. - (CGSize)sizeThatFits:(CGSize)size {
  168. CGSize sizeForTextLabel = size;
  169. sizeForTextLabel.width -= LGAlertViewPaddingWidth * 2.0;
  170. if (self.imageView.image) {
  171. sizeForTextLabel.width -= self.imageView.image.size.width + LGAlertViewButtonImageOffsetFromTitle;
  172. }
  173. CGSize labelSize = [self.textLabel sizeThatFits:size];
  174. CGSize resultSize;
  175. if (self.imageView.image) {
  176. CGSize imageSize = self.imageView.image.size;
  177. CGFloat width = MAX(labelSize.width, imageSize.width);
  178. width = MIN(width, size.width);
  179. CGFloat height = MAX(labelSize.height, imageSize.height);
  180. resultSize = CGSizeMake(width, height);
  181. }
  182. else {
  183. resultSize = labelSize;
  184. }
  185. resultSize.height += LGAlertViewPaddingHeight * 2.0;
  186. return resultSize;
  187. }
  188. - (void)clearBackgrounds {
  189. self.textLabel.backgroundColor = UIColor.clearColor;
  190. self.imageView.backgroundColor = UIColor.clearColor;
  191. }
  192. @end