XLsn0wInputBox.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #import "XLsn0wInputBox.h"
  2. #import "Masonry.h"
  3. #import "ConfigHeader.h"
  4. #define XLsn0wInputBoxWidth 300
  5. #define XLsn0wInputBoxHeight 240
  6. #define HexColor(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
  7. #define HexColorAlpha(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(a)]
  8. @interface XLsn0wInputBox ()
  9. /** 确认按钮回调 */
  10. @property (nonatomic, copy) confirmCallback confirmBlock;
  11. /** 蒙版 */
  12. @property (nonatomic, strong) UIButton *maskView;
  13. @end
  14. @implementation XLsn0wInputBox
  15. - (instancetype)init {
  16. if (self = [super init]) {
  17. }
  18. return self;
  19. }
  20. - (instancetype)initWithFrame:(CGRect)frame {
  21. if (self = [super initWithFrame:frame]) {
  22. [self initUI];
  23. }
  24. return self;
  25. }
  26. - (void)initUI {
  27. self.maskView = [[UIButton alloc] initWithFrame:[UIScreen mainScreen].bounds];
  28. self.maskView.backgroundColor = [UIColor blackColor];
  29. self.maskView.layer.opacity = 0.3;
  30. // [[self.maskView rac_signalForControlEvents:UIControlEventTouchUpInside]
  31. // subscribeNext:^(__kindof UIControl * _Nullable x) {
  32. // [self hide];
  33. // }];
  34. [self.maskView addTarget:self action:@selector(hide) forControlEvents:(UIControlEventTouchUpInside)];
  35. self.backgroundColor = UIColor.whiteColor;
  36. // [self xlsn0w_addCornerRadius:4];
  37. self.layer.cornerRadius = 4;
  38. self.layer.masksToBounds = YES;
  39. self.frame = CGRectMake(0, 0, XLsn0wInputBoxWidth, XLsn0wInputBoxHeight);
  40. self.center = CGPointMake(self.maskView.center.x, self.maskView.frame.size.height * 0.4);
  41. _title = [[UILabel alloc] init];
  42. [self addSubview:_title];
  43. [_title mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.centerX.mas_equalTo(self);
  45. make.top.mas_equalTo(16);
  46. }];
  47. // [_title addTextFont:[UIFont systemFontOfSize:16] textColor:HexColor(@"#333333")];
  48. _title.textColor = HexColor(0x333333);
  49. _title.font = AppSystemFont(16);
  50. _inputTextView = [[JHHolderTextView alloc] initWithFrame:CGRectMake(22, 48, self.frame.size.width-44, self.frame.size.height-77-48)];
  51. [self addSubview:_inputTextView];
  52. [_inputTextView mas_makeConstraints:^(MASConstraintMaker *make) {
  53. make.left.mas_equalTo(22);
  54. make.right.mas_equalTo(-22);
  55. make.top.mas_equalTo(48);
  56. make.bottom.mas_equalTo(-70);
  57. }];
  58. _inputTextView.layer.borderWidth = 1;
  59. _inputTextView.layer.borderColor = HexColor(0xE1E1E1).CGColor;
  60. _inputTextView.layer.cornerRadius = 4;
  61. _inputTextView.layer.masksToBounds = YES;
  62. // _inputTextView.holder = @"填写\"震\"内容或点击\"确定\"直接震";
  63. _inputTextView.limitCount = 50;
  64. _cancelBtn = [UIButton new];
  65. [self addSubview:_cancelBtn];
  66. [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  67. make.bottom.mas_equalTo(0);
  68. make.left.mas_equalTo(0);
  69. make.width.mas_equalTo(XLsn0wInputBoxWidth/2);
  70. make.height.mas_equalTo(55);
  71. }];
  72. [_cancelBtn setTitle:@"取消" forState:(UIControlStateNormal)];
  73. [_cancelBtn setTitleColor:HexColor(0x666666) forState:(UIControlStateNormal)];
  74. _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  75. // [[_cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside]
  76. // subscribeNext:^(__kindof UIControl * _Nullable x) {
  77. // [self hide];
  78. // }];
  79. [_cancelBtn addTarget:self action:@selector(hide) forControlEvents:(UIControlEventTouchUpInside)];
  80. _cancelBtn.backgroundColor = AppWhiteColor;
  81. ButtonHighlighted(_cancelBtn, HexColorAlpha(0x000000, 0.1))
  82. _confirmBtn = [UIButton new];
  83. [self addSubview:_confirmBtn];
  84. [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  85. make.bottom.mas_equalTo(0);
  86. make.right.mas_equalTo(0);
  87. make.width.mas_equalTo(XLsn0wInputBoxWidth/2);
  88. make.height.mas_equalTo(55);
  89. }];
  90. [_confirmBtn setTitle:@"确认" forState:(UIControlStateNormal)];
  91. [_confirmBtn setTitleColor:HexColor(0x4E8AFF) forState:(UIControlStateNormal)];
  92. _confirmBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  93. // [[_confirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside]
  94. // subscribeNext:^(__kindof UIControl * _Nullable x) {
  95. // [self confirmAction];
  96. // }];
  97. [_confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:(UIControlEventTouchUpInside)];
  98. _confirmBtn.backgroundColor = AppWhiteColor;
  99. ButtonHighlighted(_confirmBtn, HexColorAlpha(0x000000, 0.1))
  100. UIView* horizontalLine = [[UIView alloc] init];
  101. [self addSubview:horizontalLine];
  102. [horizontalLine mas_makeConstraints:^(MASConstraintMaker *make) {
  103. make.left.mas_equalTo(0);
  104. make.bottom.mas_equalTo(-55);
  105. make.width.mas_equalTo(XLsn0wInputBoxWidth);
  106. make.height.mas_equalTo(1);
  107. }];
  108. horizontalLine.backgroundColor = HexColorAlpha(0x000000, 0.1);
  109. UIView* verticalLine = [[UIView alloc] init];
  110. [self addSubview:verticalLine];
  111. [verticalLine mas_makeConstraints:^(MASConstraintMaker *make) {
  112. make.centerX.mas_equalTo(self);
  113. make.bottom.mas_equalTo(0);
  114. make.width.mas_equalTo(1);
  115. make.height.mas_equalTo(55);
  116. }];
  117. verticalLine.backgroundColor = HexColorAlpha(0x000000, 0.1);
  118. }
  119. #pragma mark - 设置控件圆角
  120. - (void)setCornerRadius:(UIView *)view {
  121. if (self.radius) {
  122. view.layer.cornerRadius = self.radius;
  123. } else {
  124. view.layer.cornerRadius = 5.0;
  125. }
  126. view.layer.masksToBounds = YES;
  127. }
  128. - (void)setPlaceholder:(NSString *)placeholder {
  129. _placeholder = [placeholder copy];
  130. // self.inputTextView.placeholder = self.placeholder;
  131. // NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor]}];
  132. // self.inputTextView.attributedPlaceholder = placeholderString;
  133. // Ivar ivar = class_getInstanceVariable([UITextView class], "_placeholderLabel");
  134. // UILabel *placehoderLabel = object_getIvar(self.inputTextView, ivar);
  135. // placehoderLabel.text = placeholder;
  136. _inputTextView.holder = placeholder;
  137. // _inputTextView.limitCount = 50;
  138. }
  139. - (void)show {
  140. [UIView animateWithDuration:0.2 animations:^{
  141. [kKeyWindow addSubview:self.maskView];
  142. [kKeyWindow addSubview:self];
  143. [self->_inputTextView.textView becomeFirstResponder];
  144. } completion:^(BOOL finished) {}];
  145. }
  146. - (void)exitKeyboard {
  147. [self endEditing:YES];
  148. }
  149. - (void)hide {
  150. [UIView animateWithDuration:0.2 animations:^{
  151. [self exitKeyboard];
  152. [self removeFromSuperview];
  153. [self.maskView removeFromSuperview];
  154. } completion:^(BOOL finished) {
  155. }];
  156. }
  157. #pragma mark - 接收传过来的block
  158. - (void)confirmBtnClickBlock:(confirmCallback)block {
  159. self.confirmBlock = block;
  160. }
  161. #pragma mark - 点击确认按钮
  162. - (void)confirmAction {
  163. [self hide];
  164. if (self.confirmBlock) {
  165. self.confirmBlock(self.inputTextView.text);
  166. }
  167. }
  168. @end