JXInputRectView.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // JXInputRectView.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 2019/9/24.
  6. // Copyright © 2019年 Reese. All rights reserved.
  7. //
  8. #import "JXInputRectView.h"
  9. #define RECT_INSET 24
  10. @interface JXInputRectView () <UITextViewDelegate>
  11. @property (nonatomic, strong) UIView *bigView;
  12. @property (nonatomic, strong) UIView *baseView;
  13. @property (nonatomic, strong) UIView *topView;
  14. @property (nonatomic, strong) UILabel *replayTitle;
  15. @property (nonatomic, strong) UITextView *replayTextView;
  16. @property (nonatomic, strong) NSString *btnTitle;
  17. @property (nonatomic, strong) UIScrollView *scrollView;
  18. @property (nonatomic, strong) UILabel *placeLabel;
  19. @end
  20. @implementation JXInputRectView
  21. - (instancetype)initWithFrame:(CGRect)frame sureBtnTitle:(NSString *)btnTitle {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. self.backgroundColor = [UIColor clearColor];
  25. self.btnTitle = btnTitle;
  26. self.isShow = YES;
  27. [self setupReplayView];
  28. }
  29. return self;
  30. }
  31. - (void)setupReplayView {
  32. self.bigView = [[UIView alloc] initWithFrame:self.bounds];
  33. self.bigView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
  34. [self addSubview:self.bigView];
  35. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
  36. [self.bigView addGestureRecognizer:tap];
  37. self.baseView = [[UIView alloc] initWithFrame:CGRectMake(40, JX_SCREEN_HEIGHT/4-.5, JX_SCREEN_WIDTH-80, 162.5)];
  38. self.baseView.backgroundColor = [UIColor whiteColor];
  39. self.baseView.layer.masksToBounds = YES;
  40. self. baseView.layer.cornerRadius = 4.0f;
  41. [self.bigView addSubview:self.baseView];
  42. int n = 20;
  43. _replayTitle = [[UILabel alloc] initWithFrame:CGRectMake(INSETS, n, self.baseView.frame.size.width - INSETS*2, 20)];
  44. _replayTitle.lineBreakMode = NSLineBreakByTruncatingTail;
  45. _replayTitle.textColor = HEXCOLOR(0x333333);
  46. _replayTitle.font = SYSFONT(16);
  47. _replayTitle.numberOfLines = 0;
  48. [self.baseView addSubview:_replayTitle];
  49. self.replayTextView = [self createTextField:self.baseView default:nil hint:nil];
  50. self.replayTextView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
  51. self.replayTextView.frame = CGRectMake(INSETS, CGRectGetMaxY(_replayTitle.frame)+RECT_INSET, self.baseView.frame.size.width - INSETS*2, 35.5);
  52. self.replayTextView.delegate = self;
  53. self.replayTextView.textColor = HEXCOLOR(0x595959);
  54. self.replayTextView.font = SYSFONT(16);
  55. n = n + 54;
  56. self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, n, self.baseView.frame.size.width, 44)];
  57. [self.baseView addSubview:self.topView];
  58. [self.replayTextView becomeFirstResponder];
  59. // 水印
  60. self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(5, 8, self.replayTextView.frame.size.width-10, self.replayTextView.frame.size.height)];
  61. self.scrollView.hidden = YES;
  62. [self.replayTextView addSubview:self.scrollView];
  63. UITapGestureRecognizer *tapScr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(inputTextView)];
  64. [self.scrollView addGestureRecognizer:tapScr];
  65. self.placeLabel = [[UILabel alloc] initWithFrame:self.scrollView.bounds];
  66. self.placeLabel.textColor = HEXCOLOR(0x999999);
  67. self.placeLabel.numberOfLines = 0;
  68. self.placeLabel.font = SYSFONT(16);
  69. [self.scrollView addSubview:self.placeLabel];
  70. // 两条线
  71. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.baseView.frame.size.width, LINE_WH)];
  72. topLine.backgroundColor = THE_LINE_COLOR;
  73. [self.topView addSubview:topLine];
  74. UIView *botLine = [[UIView alloc] initWithFrame:CGRectMake(self.baseView.frame.size.width/2, 0, LINE_WH, self.topView.frame.size.height)];
  75. botLine.backgroundColor = THE_LINE_COLOR;
  76. [self.topView addSubview:botLine];
  77. // 取消
  78. UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(topLine.frame), self.baseView.frame.size.width/2, botLine.frame.size.height)];
  79. [cancelBtn setTitle:Localized(@"JX_Cencal") forState:UIControlStateNormal];
  80. [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  81. [cancelBtn.titleLabel setFont:SYSFONT(15)];
  82. [cancelBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  83. [self.topView addSubview:cancelBtn];
  84. // 发送
  85. UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.baseView.frame.size.width/2, CGRectGetMaxY(topLine.frame), self.baseView.frame.size.width/2, botLine.frame.size.height)];
  86. [sureBtn setTitle:self.btnTitle forState:UIControlStateNormal];
  87. [sureBtn setTitleColor:HEXCOLOR(0x55BEB8) forState:UIControlStateNormal];
  88. [sureBtn.titleLabel setFont:SYSFONT(15)];
  89. [sureBtn addTarget:self action:@selector(onSend) forControlEvents:UIControlEventTouchUpInside];
  90. [self.topView addSubview:sureBtn];
  91. self.placeString = @"";
  92. }
  93. - (void)setPlaceString:(NSString *)placeString {
  94. _placeString = placeString;
  95. if (_placeString.length > 0) {
  96. self.scrollView.hidden = NO;
  97. self.placeLabel.text = placeString;
  98. }else {
  99. self.scrollView.hidden = YES;
  100. }
  101. CGSize size = [placeString boundingRectWithSize:CGSizeMake(self.placeLabel.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.placeLabel.font} context:nil].size;
  102. if (size.height < 19) {
  103. size.height = 19;
  104. }
  105. [self setViewFrameWithSize:size.height];
  106. }
  107. - (void)setViewFrameWithSize:(CGFloat)height; {
  108. self.scrollView.contentSize = CGSizeMake(0, height);
  109. self.placeLabel.frame = CGRectMake(self.placeLabel.frame.origin.x, self.placeLabel.frame.origin.y, self.placeLabel.frame.size.width, height);
  110. if (height > 66) {
  111. height = 66;
  112. }
  113. self.scrollView.frame = CGRectMake(self.scrollView.frame.origin.x, self.scrollView.frame.origin.y, self.scrollView.frame.size.width, height);
  114. self.replayTextView.frame = CGRectMake(self.replayTextView.frame.origin.x, CGRectGetMaxY(self.replayTitle.frame)+RECT_INSET, self.replayTextView.frame.size.width, height+16);
  115. self.topView.frame = CGRectMake(0, CGRectGetMaxY(self.replayTextView.frame)+24, self.baseView.frame.size.width, self.topView.frame.size.height);
  116. self.baseView.frame = CGRectMake(40, JX_SCREEN_HEIGHT/4+35-self.replayTextView.frame.size.height, JX_SCREEN_WIDTH-80, CGRectGetMaxY(self.topView.frame));
  117. }
  118. - (void)textViewDidChange:(UITextView *)textView {
  119. if (textView.text.length > 0) {
  120. self.scrollView.hidden = YES;
  121. }else {
  122. self.scrollView.hidden = NO;
  123. CGSize size = [self.placeLabel.text boundingRectWithSize:CGSizeMake(self.placeLabel.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.placeLabel.font} context:nil].size;
  124. [self setViewFrameWithSize:size.height];
  125. return;
  126. }
  127. static CGFloat maxHeight =66.0f;
  128. CGRect frame = textView.frame;
  129. CGSize constraintSize = CGSizeMake(JX_SCREEN_WIDTH-80-INSETS*2, MAXFLOAT);
  130. CGSize size = [textView sizeThatFits:constraintSize];
  131. if (size.height >= maxHeight)
  132. {
  133. size.height = maxHeight;
  134. textView.scrollEnabled = YES; // 允许滚动
  135. }
  136. else
  137. {
  138. textView.scrollEnabled = NO; // 不允许滚动
  139. }
  140. // [self setViewFrameWithSize:size.height];
  141. textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
  142. NSLog(@"--------%@",NSStringFromCGRect(self.baseView.frame));
  143. self.topView.frame = CGRectMake(0, CGRectGetMaxY(textView.frame)+24, self.baseView.frame.size.width, self.topView.frame.size.height);
  144. self.baseView.frame = CGRectMake(40, JX_SCREEN_HEIGHT/4+35-size.height, JX_SCREEN_WIDTH-80, CGRectGetMaxY(self.topView.frame));
  145. }
  146. - (NSString *)text {
  147. return self.replayTextView.text;
  148. }
  149. - (void)inputTextView {
  150. if (![self.replayTextView isFirstResponder]) {
  151. [self.replayTextView becomeFirstResponder];
  152. }
  153. }
  154. - (void)onSend {
  155. if (self.delegate && [self.delegate respondsToSelector:self.onRelease]) {
  156. [self.delegate performSelectorOnMainThread:self.onRelease withObject:nil waitUntilDone:NO];
  157. }
  158. }
  159. - (void)hide {
  160. self.isShow = NO;
  161. [self removeFromSuperview];
  162. }
  163. - (void)dealloc {
  164. }
  165. - (void)hideKeyBoard {
  166. if (self.replayTextView.isFirstResponder) {
  167. [self.replayTextView resignFirstResponder];
  168. }
  169. }
  170. - (void)setBackColor:(UIColor *)backColor{
  171. _backColor = backColor;
  172. self.bigView.backgroundColor = backColor;
  173. }
  174. - (void)setTitle:(NSString *)title {
  175. _title = title;
  176. _replayTitle.text = title;
  177. CGSize size = [_replayTitle.text boundingRectWithSize:CGSizeMake(_replayTitle.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:_replayTitle.font} context:nil].size;
  178. CGRect frame = _replayTitle.frame;
  179. frame.size = size;
  180. _replayTitle.frame = frame;
  181. if (size.height > 30) {
  182. [self textViewDidChange:self.replayTextView];
  183. }
  184. }
  185. - (void)setIsTitleCenter:(BOOL)isTitleCenter {
  186. _isTitleCenter = isTitleCenter;
  187. if (isTitleCenter) {
  188. _replayTitle.center = CGPointMake(self.baseView.frame.size.width / 2, _replayTitle.center.y);
  189. }
  190. }
  191. -(UITextView*)createTextField:(UIView*)parent default:(NSString*)s hint:(NSString*)hint{
  192. UITextView* p = [[UITextView alloc] initWithFrame:CGRectMake(0,INSETS,JX_SCREEN_WIDTH,54)];
  193. p.delegate = self;
  194. p.autocorrectionType = UITextAutocorrectionTypeNo;
  195. p.autocapitalizationType = UITextAutocapitalizationTypeNone;
  196. p.enablesReturnKeyAutomatically = YES;
  197. p.scrollEnabled = NO;
  198. p.showsVerticalScrollIndicator = NO;
  199. p.showsHorizontalScrollIndicator = NO;
  200. p.textAlignment = NSTextAlignmentLeft;
  201. p.userInteractionEnabled = YES;
  202. p.backgroundColor = [UIColor whiteColor];
  203. p.text = s;
  204. p.font = g_factory.font16;
  205. [parent addSubview:p];
  206. return p;
  207. }
  208. @end