CustomCheckBox.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. } else if (type == 2) {
  55. _imgNormal = [UIImage imageNamed:@"common_checkbox_cal"];
  56. _imgHighlighted = [UIImage imageNamed:@"common_checkbox_cal_checked"];
  57. } else if (type == 3) {
  58. _imgNormal = [UIImage imageNamed:@"common_checkbox_cal_last"];
  59. _imgHighlighted = [UIImage imageNamed:@"common_checkbox_cal_last_checked"];
  60. }
  61. [self setImage:_imgNormal forState:UIControlStateNormal];
  62. [self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
  63. }
  64. - (IBAction)checkBoxClicked {
  65. if (self.checked == NO) {
  66. self.checked = YES;
  67. } else {
  68. self.checked = NO;
  69. }
  70. if ([self.delegate respondsToSelector:@selector(didCheckBoxClicked:)]) {
  71. [self.delegate didCheckBoxClicked:self];
  72. }
  73. }
  74. - (BOOL)isChecked {
  75. return _checked;
  76. }
  77. - (BOOL)checked {
  78. return _disable ? NO : _checked;
  79. }
  80. //체크 On/Off
  81. - (void)setChecked:(BOOL)checked {
  82. _checked = checked;
  83. if (_checked) {
  84. [self setImage:_imgHighlighted forState:UIControlStateNormal];
  85. [self setTitleColor:_textColorHighlight forState:UIControlStateNormal];
  86. } else {
  87. [self setImage:_imgNormal forState:UIControlStateNormal];
  88. [self setTitleColor:_textColorNormal forState:UIControlStateNormal];
  89. }
  90. if (_value) {
  91. objc_setAssociatedObject(_value, ksCustomCheckBoxStatus, [NSNumber numberWithBool:_checked], OBJC_ASSOCIATION_COPY_NONATOMIC);
  92. }
  93. }
  94. - (BOOL)getCheckStatusFromValue {
  95. BOOL isChecked = NO;
  96. if (_value) {
  97. isChecked = [objc_getAssociatedObject(_value, ksCustomCheckBoxStatus) boolValue];
  98. }
  99. return isChecked;
  100. }
  101. //disable On/Off
  102. - (void)setDisable:(BOOL)disable {
  103. _disable = _checked = disable;
  104. if (_disable && _imgDisbale) {
  105. [self setImage:_imgDisbale forState:UIControlStateNormal];
  106. } else {
  107. [self setCheckedDisable:_checkedDisable];
  108. }
  109. }
  110. //체크된 상태에서 On/Off
  111. - (void)setCheckedDisable:(BOOL)checkedDisable {
  112. _checkedDisable = _checked = checkedDisable;
  113. if (_checkedDisable) {
  114. [self setImage:_imgDisbale forState:UIControlStateNormal];
  115. } else {
  116. [self setImage:_imgNormal forState:UIControlStateNormal];
  117. }
  118. }
  119. @end