CustomCheckBox.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. - (IBAction)checkBoxClicked {
  50. if (self.checked == NO) {
  51. self.checked = YES;
  52. } else {
  53. self.checked = NO;
  54. }
  55. if ([self.delegate respondsToSelector:@selector(didCheckBoxClicked:)]) {
  56. [self.delegate didCheckBoxClicked:self];
  57. }
  58. }
  59. - (BOOL)isChecked {
  60. return _checked;
  61. }
  62. - (BOOL)checked {
  63. return _disable ? NO : _checked;
  64. }
  65. //체크 On/Off
  66. - (void)setChecked:(BOOL)checked {
  67. _checked = checked;
  68. if (_checked) {
  69. [self setImage:_imgHighlighted forState:UIControlStateNormal];
  70. [self setTitleColor:_textColorHighlight forState:UIControlStateNormal];
  71. } else {
  72. [self setImage:_imgNormal forState:UIControlStateNormal];
  73. [self setTitleColor:_textColorNormal forState:UIControlStateNormal];
  74. }
  75. if (_value) {
  76. objc_setAssociatedObject(_value, ksCustomCheckBoxStatus, [NSNumber numberWithBool:_checked], OBJC_ASSOCIATION_COPY_NONATOMIC);
  77. }
  78. }
  79. - (BOOL)getCheckStatusFromValue {
  80. BOOL isChecked = NO;
  81. if (_value) {
  82. isChecked = [objc_getAssociatedObject(_value, ksCustomCheckBoxStatus) boolValue];
  83. }
  84. return isChecked;
  85. }
  86. //disable On/Off
  87. - (void)setDisable:(BOOL)disable {
  88. _disable = _checked = disable;
  89. if (_disable && _imgDisbale) {
  90. [self setImage:_imgDisbale forState:UIControlStateNormal];
  91. } else {
  92. [self setCheckedDisable:_checkedDisable];
  93. }
  94. }
  95. //체크된 상태에서 On/Off
  96. - (void)setCheckedDisable:(BOOL)checkedDisable {
  97. _checkedDisable = _checked = checkedDisable;
  98. if (_checkedDisable) {
  99. [self setImage:_imgDisbale forState:UIControlStateNormal];
  100. } else {
  101. [self setImage:_imgNormal forState:UIControlStateNormal];
  102. }
  103. }
  104. @end