CustomCheckBox.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. @interface CustomCheckBox () {
  11. UIImage *_imgNormal, *_imgHighlighted, *_imgDisbale;
  12. UIColor *_textColorNormal, *_textColorHighlight;
  13. }
  14. @end
  15. @implementation CustomCheckBox
  16. @synthesize checked = _checked;
  17. - (id)initWithFrame:(CGRect)frame {//소스레벨에서 로드하는 경우, 초기화
  18. if (self = [super initWithFrame:frame]) {
  19. // Initialization code
  20. _imgNormal = [UIImage imageNamed:@"common_checkbox_default"];
  21. _imgHighlighted = [UIImage imageNamed:@"common_checkbox_checked"];
  22. _imgDisbale = [UIImage imageNamed:@"common_checkbox_disable"];
  23. self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  24. [self setImage:_imgNormal forState:UIControlStateNormal];
  25. [self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
  26. }
  27. return self;
  28. }
  29. - (instancetype)initWithFrame:(CGRect)frame normalImage:(UIImage *)normalImage highlightImage:(UIImage *)highlightImage {//소스레벨에서 로드하는 경우, 초기화
  30. if (self = [super initWithFrame:frame]) {
  31. _imgNormal = normalImage;
  32. _imgHighlighted = highlightImage;
  33. self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  34. [self setImage:_imgNormal forState:UIControlStateNormal];
  35. [self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
  36. }
  37. return self;
  38. }
  39. - (void)awakeFromNib {//NIB에서 로드하는 경우, 초기화
  40. self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  41. _imgNormal = [UIImage imageNamed:@"common_checkbox_default"];
  42. _imgHighlighted = [UIImage imageNamed:@"common_checkbox_checked"];
  43. _imgDisbale = [UIImage imageNamed:@"common_checkbox_disable"];
  44. _textColorNormal = [self titleColorForState:UIControlStateNormal];
  45. _textColorHighlight = [self titleColorForState:UIControlStateHighlighted];
  46. [self setImage:_imgNormal forState:UIControlStateNormal];
  47. [self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
  48. }
  49. - (void)setBgImage{
  50. _imgNormal = [UIImage imageNamed:@"common_list_btn_checkbox_default"];
  51. _imgHighlighted = [UIImage imageNamed:@"common_list_btn_checkbox_press"];
  52. [self setImage:_imgNormal forState:UIControlStateNormal];
  53. [self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
  54. }
  55. - (IBAction)checkBoxClicked {
  56. if (self.checked == NO) {
  57. self.checked = YES;
  58. } else {
  59. self.checked = NO;
  60. }
  61. if ([self.delegate respondsToSelector:@selector(didCheckBoxClicked:)]) {
  62. [self.delegate didCheckBoxClicked:self];
  63. }
  64. }
  65. - (BOOL)isChecked {
  66. return _checked;
  67. }
  68. - (BOOL)checked {
  69. return _disable ? NO : _checked;
  70. }
  71. //체크 On/Off
  72. - (void)setChecked:(BOOL)checked {
  73. _checked = checked;
  74. if (_checked) {
  75. [self setImage:_imgHighlighted forState:UIControlStateNormal];
  76. [self setTitleColor:_textColorHighlight forState:UIControlStateNormal];
  77. } else {
  78. [self setImage:_imgNormal forState:UIControlStateNormal];
  79. [self setTitleColor:_textColorNormal forState:UIControlStateNormal];
  80. }
  81. if (_value) {
  82. objc_setAssociatedObject(_value, ksCustomCheckBoxStatus, [NSNumber numberWithBool:_checked], OBJC_ASSOCIATION_COPY_NONATOMIC);
  83. }
  84. }
  85. - (BOOL)getCheckStatusFromValue {
  86. BOOL isChecked = NO;
  87. if (_value) {
  88. isChecked = [objc_getAssociatedObject(_value, ksCustomCheckBoxStatus) boolValue];
  89. }
  90. return isChecked;
  91. }
  92. //disable On/Off
  93. - (void)setDisable:(BOOL)disable {
  94. _disable = _checked = disable;
  95. if (_disable && _imgDisbale) {
  96. [self setImage:_imgDisbale forState:UIControlStateNormal];
  97. } else {
  98. [self setCheckedDisable:_checkedDisable];
  99. }
  100. }
  101. //체크된 상태에서 On/Off
  102. - (void)setCheckedDisable:(BOOL)checkedDisable {
  103. _checkedDisable = _checked = checkedDisable;
  104. if (_checkedDisable) {
  105. [self setImage:_imgDisbale forState:UIControlStateNormal];
  106. } else {
  107. [self setImage:_imgNormal forState:UIControlStateNormal];
  108. }
  109. }
  110. @end