CustomCheckBox.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // UICheckBox.m
  3. // JasonDevelop
  4. //
  5. // Created by Hslee on 10. 11. 22..
  6. // Copyright 2013 AnchorData. All rights reserved.
  7. //
  8. @import ObjectiveC.runtime;
  9. #import "CustomCheckBox.h"
  10. #import "CustomLabel.h"
  11. @interface CustomCheckBox () {
  12. UIImage *_imgNormal, *_imgHighlighted, *_imgDisbale;
  13. UIColor *_textColorNormal, *_textColorHighlight;
  14. }
  15. @end
  16. @implementation CustomCheckBox
  17. @synthesize checked = _checked;
  18. - (id)initWithFrame:(CGRect)frame {//소스레벨에서 로드하는 경우, 초기화
  19. if (self = [super initWithFrame:frame]) {
  20. // Initialization code
  21. _imgNormal = [UIImage imageNamed:@"common_checkbox_default"];
  22. _imgHighlighted = [UIImage imageNamed:@"common_checkbox_checked"];
  23. _imgDisbale = [UIImage imageNamed:@"common_checkbox_disable"];
  24. self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  25. [self setImage:_imgNormal forState:UIControlStateNormal];
  26. [self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
  27. }
  28. return self;
  29. }
  30. - (instancetype)initWithFrame:(CGRect)frame normalImage:(UIImage *)normalImage highlightImage:(UIImage *)highlightImage {//소스레벨에서 로드하는 경우, 초기화
  31. if (self = [super initWithFrame:frame]) {
  32. _imgNormal = normalImage;
  33. _imgHighlighted = highlightImage;
  34. self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  35. [self setImage:_imgNormal forState:UIControlStateNormal];
  36. [self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
  37. }
  38. return self;
  39. }
  40. - (void)awakeFromNib {//NIB에서 로드하는 경우, 초기화
  41. self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  42. _imgNormal = [UIImage imageNamed:@"common_checkbox_default"];
  43. _imgHighlighted = [UIImage imageNamed:@"common_checkbox_checked"];
  44. _imgDisbale = [UIImage imageNamed:@"common_checkbox_disable"];
  45. _textColorNormal = [self titleColorForState:UIControlStateNormal];
  46. _textColorHighlight = [self titleColorForState:UIControlStateHighlighted];
  47. [self setImage:_imgNormal forState:UIControlStateNormal];
  48. [self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
  49. }
  50. - (void)setDaySelectBgImage:(NSInteger)type{
  51. if (type == 1) {
  52. _imgNormal = [UIImage imageNamed:@"common_checkbox_cal_first"];
  53. _imgHighlighted = [UIImage imageNamed:@"common_checkbox_cal_first_checked"];
  54. }
  55. else if (type == 2) {
  56. _imgNormal = [UIImage imageNamed:@"common_checkbox_cal"];
  57. _imgHighlighted = [UIImage imageNamed:@"common_checkbox_cal_checked"];
  58. }
  59. else if (type == 3) {
  60. _imgNormal = [UIImage imageNamed:@"common_checkbox_cal_last"];
  61. _imgHighlighted = [UIImage imageNamed:@"common_checkbox_cal_last_checked"];
  62. }
  63. else if (type == 4) {
  64. _imgNormal = [UIImage imageNamed:@"common_radiobox_default"];
  65. _imgHighlighted = [UIImage imageNamed:@"common_radiobox_checked"];
  66. }
  67. [self setImage:_imgNormal forState:UIControlStateNormal];
  68. [self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
  69. }
  70. - (IBAction)checkBoxClicked {
  71. if (self.checked == NO) {
  72. self.checked = YES;
  73. } else {
  74. self.checked = NO;
  75. }
  76. if ([self.delegate respondsToSelector:@selector(didCheckBoxClicked:)]) {
  77. [self.delegate didCheckBoxClicked:self];
  78. }
  79. }
  80. - (BOOL)isChecked {
  81. return _checked;
  82. }
  83. - (BOOL)checked {
  84. return _disable ? NO : _checked;
  85. }
  86. //체크 On/Off
  87. - (void)setChecked:(BOOL)checked {
  88. _checked = checked;
  89. if (_checked) {
  90. [self setImage:_imgHighlighted forState:UIControlStateNormal];
  91. [self setTitleColor:_textColorHighlight forState:UIControlStateNormal];
  92. } else {
  93. [self setImage:_imgNormal forState:UIControlStateNormal];
  94. [self setTitleColor:_textColorNormal forState:UIControlStateNormal];
  95. }
  96. if (_value) {
  97. objc_setAssociatedObject(_value, ksCustomCheckBoxStatus, [NSNumber numberWithBool:_checked], OBJC_ASSOCIATION_COPY_NONATOMIC);
  98. }
  99. }
  100. - (BOOL)getCheckStatusFromValue {
  101. BOOL isChecked = NO;
  102. if (_value) {
  103. isChecked = [objc_getAssociatedObject(_value, ksCustomCheckBoxStatus) boolValue];
  104. }
  105. return isChecked;
  106. }
  107. //disable On/Off
  108. - (void)setDisable:(BOOL)disable {
  109. _disable = _checked = disable;
  110. if (_disable && _imgDisbale) {
  111. [self setImage:_imgDisbale forState:UIControlStateNormal];
  112. } else {
  113. [self setCheckedDisable:_checkedDisable];
  114. }
  115. }
  116. //체크된 상태에서 On/Off
  117. - (void)setCheckedDisable:(BOOL)checkedDisable {
  118. _checkedDisable = _checked = checkedDisable;
  119. if (_checkedDisable) {
  120. [self setImage:_imgDisbale forState:UIControlStateNormal];
  121. } else {
  122. [self setImage:_imgNormal forState:UIControlStateNormal];
  123. }
  124. }
  125. @end