CustomBoxView.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // CustomBoxView.m
  3. // MIT_Endorsement
  4. //
  5. // Created by 翟玉磊 on 2017/9/21.
  6. // Copyright © 2017年 翟玉磊. All rights reserved.
  7. //
  8. #import "CustomBoxView.h"
  9. @interface CustomBoxView ()
  10. @property (nonatomic, strong) UIButton *button;
  11. @property (nonatomic, strong) UIView *topLine;
  12. @property (nonatomic, strong) UIView *bottomLine;
  13. @end
  14. @implementation CustomBoxView
  15. - (instancetype)initWithFrame:(CGRect)frame {
  16. if (self = [super initWithFrame:frame]) {
  17. [self initValue];
  18. [self buildView];
  19. }
  20. return self;
  21. }
  22. - (instancetype)init {
  23. if (self = [super init]) {
  24. [self initValue];
  25. [self buildView];
  26. }
  27. return self;
  28. }
  29. - (void)initValue {
  30. self.backgroundColor = [UIColor whiteColor];
  31. self.isClick = NO;
  32. self.isShowMoreImage = NO;
  33. self.isShowBottomLine = YES;
  34. self.button.hidden = YES;
  35. self.moreImageView.hidden = YES;
  36. self.bottomLine.hidden = NO;
  37. self.isShowTopLine = NO;
  38. self.topLine.hidden = YES; //顶部分割线默认隐藏
  39. }
  40. - (void)buildView {
  41. [self addSubview:self.topLine];
  42. [self addSubview:self.titleLabel];
  43. [self addSubview:self.moreImageView];
  44. [self addSubview:self.bottomLine];
  45. [self addSubview:self.button];
  46. [self.topLine mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.left.right.top.equalTo(self);
  48. make.height.equalTo(@0.5);
  49. }];
  50. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.left.equalTo(self).offset(SPACING_EDGE);
  52. make.centerY.equalTo(self);
  53. }];
  54. [self.moreImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  55. make.right.equalTo(self).offset(-SPACING_EDGE);
  56. make.centerY.equalTo(self);
  57. make.width.equalTo(@8.0f);
  58. make.height.equalTo(@13.0f);
  59. }];
  60. [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.left.right.bottom.equalTo(self);
  62. make.height.equalTo(@0.5);
  63. }];
  64. [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.left.right.bottom.top.equalTo(self);
  66. }];
  67. }
  68. #pragma mark - Public
  69. - (void)setTitle:(NSString *)title {
  70. _title = title;
  71. self.titleLabel.text = title;
  72. }
  73. - (void)setTitleColor:(UIColor *)titleColor {
  74. _titleColor = titleColor;
  75. self.titleLabel.textColor = titleColor;
  76. }
  77. - (void)setIsClick:(BOOL)isClick {
  78. _isClick = isClick;
  79. self.button.hidden = !isClick;
  80. }
  81. - (void)setIsShowMoreImage:(BOOL)isShowMoreImage {
  82. _isShowMoreImage = isShowMoreImage;
  83. self.moreImageView.hidden = !isShowMoreImage;
  84. }
  85. - (void)setIsShowTopLine:(BOOL)isShowTopLine {
  86. _isShowTopLine = isShowTopLine;
  87. self.topLine.hidden = !isShowTopLine;
  88. }
  89. - (void)setIsShowBottomLine:(BOOL)isShowBottomLine {
  90. _isShowBottomLine = isShowBottomLine;
  91. self.bottomLine.hidden = !isShowBottomLine;
  92. }
  93. - (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents {
  94. self.button.hidden = NO;
  95. [self.button addTarget:target action:action forControlEvents:controlEvents];
  96. }
  97. - (void)setFont:(UIFont *)font {
  98. _font = font;
  99. self.titleLabel.font = font;
  100. }
  101. - (void)setMoreImag:(UIImage *)moreImag {
  102. _moreImag = moreImag;
  103. self.moreImageView.image = moreImag;
  104. }
  105. #pragma mark - Getter
  106. - (UILabel *)titleLabel {
  107. if (_titleLabel == nil) {
  108. _titleLabel = [UILabel new];
  109. _titleLabel.font = Font(15);
  110. _titleLabel.textColor = Color_TextFont;
  111. }
  112. return _titleLabel;
  113. }
  114. - (UIImageView *)moreImageView {
  115. if (_moreImageView == nil) {
  116. _moreImageView = [UIImageView new];
  117. _moreImageView.image = arrowMore();
  118. }
  119. return _moreImageView;
  120. }
  121. - (UIButton *)button {
  122. if (_button == nil) {
  123. _button = [UIButton buttonWithType:UIButtonTypeCustom];
  124. [_button setBackgroundColor:[UIColor clearColor]];
  125. }
  126. return _button;
  127. }
  128. - (UIView *)bottomLine {
  129. if (_bottomLine == nil) {
  130. _bottomLine = [UIView new];
  131. _bottomLine.backgroundColor = Color_line;
  132. }
  133. return _bottomLine;
  134. }
  135. - (UIView *)topLine {
  136. if (_topLine == nil) {
  137. _topLine = [UIView new];
  138. _topLine.backgroundColor = Color_line;
  139. }
  140. return _topLine;
  141. }
  142. @end