YMInputPopupView.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. //
  2. // YMInputPopupView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/2/25.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMInputPopupView.h"
  9. @interface YMInputPopupView ()<YYTextViewDelegate>
  10. /** 标题标签*/
  11. @property (nonatomic, strong) UILabel *titleLb;
  12. /** 内容文本框*/
  13. @property (nonatomic, strong) YYTextView *contentTextView;
  14. /** 上限标签*/
  15. @property (nonatomic, strong) UILabel *limitLb;
  16. /** 按钮视图*/
  17. @property (nonatomic, strong) UIView *buttonView;
  18. /** 取消按钮*/
  19. @property (nonatomic, strong) UIButton *cancelBtn;
  20. /** 确认按钮*/
  21. @property (nonatomic, strong) UIButton *confirmBtn;
  22. /** 单确认按钮*/
  23. @property (nonatomic, strong) UIButton *singleConfirmBtn;
  24. @end
  25. @implementation YMInputPopupView
  26. #pragma mark - private
  27. - (void)ym_setupViews{
  28. self.backgroundColor = HexColorFromRGB(0xFFFFFF);
  29. self.titleText = @"提示";
  30. self.titleFont = LCBoldFont(18);
  31. self.titleColor = HexColorFromRGB(0x333333);
  32. self.cancelBgColor = HexColorFromRGB(0xF5F5F5);
  33. self.confirmBgColor = HexColorFromRGB(0xF888E7);
  34. self.cancelTitleColor = HexColorFromRGB(0x555555);
  35. self.confirmTitleColor = HexColorFromRGB(0xFFFFFF);
  36. self.cancelFont = LCFont(14);
  37. self.confirmFont = LCFont(14);
  38. self.cancelTitle = @"取消";
  39. self.confirmTitle = @"确定";
  40. self.cancelRadius = 10;
  41. self.confirmRadius = 10;
  42. self.cancelBorderColor = HexColorFromRGB(0xF5F5F5);
  43. self.confirmBorderColor = HexColorFromRGB(0xF888E7);
  44. self.cancelBorderWidth = 0.5;
  45. self.confirmBorderWidth = 0.5;
  46. self.maxLength = 15;
  47. [self addSubview:self.titleLb];
  48. [self addSubview:self.contentTextView];
  49. [self addSubview:self.limitLb];
  50. [self addSubview:self.buttonView];
  51. [self.buttonView addSubview:self.cancelBtn];
  52. [self.buttonView addSubview:self.confirmBtn];
  53. [self addSubview:self.singleConfirmBtn];
  54. [self setNeedsUpdateConstraints];
  55. [self updateConstraintsIfNeeded];
  56. }
  57. - (void)updateConstraints{
  58. [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.top.equalTo(self).offset(adapt(15));
  60. make.left.equalTo(self).offset(adapt(20));
  61. make.right.equalTo(self).offset(adapt(-20));
  62. make.height.mas_equalTo(adapt(20));
  63. }];
  64. [self.contentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.top.equalTo(self.titleLb.mas_bottom).offset(adapt(10));
  66. make.left.equalTo(self).offset(adapt(20));
  67. make.right.equalTo(self).offset(adapt(-20));
  68. make.height.mas_equalTo(adapt(160));
  69. }];
  70. [self.limitLb mas_makeConstraints:^(MASConstraintMaker *make) {
  71. make.top.equalTo(self.contentTextView.mas_bottom).offset(adapt(10));
  72. make.right.equalTo(self.contentTextView.mas_right);
  73. make.height.mas_equalTo(adapt(15));
  74. }];
  75. [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.top.equalTo(self.limitLb.mas_bottom).offset(adapt(10));
  77. make.left.equalTo(self);
  78. make.right.equalTo(self);
  79. make.bottom.equalTo(self).offset(adapt(-15)).priorityHigh();
  80. }];
  81. NSArray *btnArr = @[self.cancelBtn,self.confirmBtn];
  82. // 实现masonry水平固定控件宽度方法
  83. // axisType 横排还是竖排
  84. // fixedSpacing 两个控件间隔
  85. // leadSpacing 第一个控件与边缘的间隔
  86. // tailSpacing 最后一个控件与边缘的间隔
  87. [btnArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:adapt(15) leadSpacing:adapt(20) tailSpacing:adapt(20)];
  88. // 设置array的垂直方向的约束
  89. [btnArr mas_makeConstraints:^(MASConstraintMaker *make) {
  90. make.top.equalTo(self.buttonView);
  91. make.bottom.equalTo(self.buttonView);
  92. make.height.mas_equalTo(adapt(38));
  93. }];
  94. [self.singleConfirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  95. make.top.equalTo(self.limitLb.mas_bottom).offset(adapt(10));
  96. make.left.equalTo(self).offset(adapt(45));
  97. make.right.equalTo(self).offset(adapt(-45));
  98. make.bottom.equalTo(self).offset(adapt(-15)).priorityHigh();
  99. make.height.mas_equalTo(adapt(38));
  100. }];
  101. [super updateConstraints];
  102. }
  103. - (void)configutationWithContentText:(NSString*)contentText ofInputHeight:(CGFloat)inputHeight IsHideTitle:(BOOL)isHideTitle IsHideSingleButton:(BOOL)isHideSingleButton{
  104. self.contentTextView.text = contentText;
  105. [self judgeTitleIsHide:isHideTitle];
  106. [self judgeSingleButtonIsHide:isHideSingleButton];
  107. [self.contentTextView mas_updateConstraints:^(MASConstraintMaker *make) {
  108. make.height.mas_equalTo(inputHeight);
  109. }];
  110. CGFloat titleLbHeight = [self.titleLb systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
  111. CGFloat contentTextViewHeight = [self.contentTextView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
  112. CGFloat limitLbHeight = [self.limitLb systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
  113. CGFloat buttonViewHeight = isHideSingleButton ? [self.singleConfirmBtn systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height : [self.buttonView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
  114. self.frame = CGRectMake(0, 0, kFrameWidth*0.8, adapt(15) + (isHideTitle ? 0 : titleLbHeight + adapt(10)) + contentTextViewHeight + adapt(10) + limitLbHeight + adapt(10) + buttonViewHeight + adapt(15));
  115. }
  116. #pragma mark - 判断标题是否隐藏
  117. - (void)judgeTitleIsHide:(BOOL)isHide{
  118. self.titleLb.hidden = isHide;
  119. if (isHide) {
  120. [self.titleLb mas_remakeConstraints:^(MASConstraintMaker *make) {
  121. }];
  122. [self.contentTextView mas_updateConstraints:^(MASConstraintMaker *make) {
  123. make.top.equalTo(self).offset(adapt(15));
  124. }];
  125. }else{
  126. [self.titleLb mas_remakeConstraints:^(MASConstraintMaker *make) {
  127. make.top.equalTo(self).offset(adapt(15));
  128. make.left.equalTo(self).offset(adapt(20));
  129. make.right.equalTo(self).offset(adapt(-20));
  130. }];
  131. [self.contentTextView mas_updateConstraints:^(MASConstraintMaker *make) {
  132. make.top.equalTo(self.titleLb.mas_bottom).offset(adapt(10));
  133. }];
  134. }
  135. }
  136. #pragma mark - 判断单按钮是否隐藏
  137. - (void)judgeSingleButtonIsHide:(BOOL)isHide{
  138. self.singleConfirmBtn.hidden = isHide;
  139. if (isHide) {
  140. self.buttonView.hidden = NO;
  141. }else{
  142. self.buttonView.hidden = YES;
  143. }
  144. }
  145. - (void)setTitleText:(NSString *)titleText{
  146. _titleText = titleText;
  147. self.titleLb.text = titleText;
  148. }
  149. - (void)setTitleFont:(UIFont *)titleFont{
  150. _titleFont = titleFont;
  151. self.titleLb.font = titleFont;
  152. }
  153. - (void)setTitleColor:(UIColor *)titleColor{
  154. _titleColor = titleColor;
  155. self.titleLb.textColor = titleColor;
  156. }
  157. - (void)setCancelBgColor:(UIColor *)cancelBgColor{
  158. _cancelBgColor = cancelBgColor;
  159. _cancelBtn.backgroundColor = cancelBgColor;
  160. }
  161. - (void)setConfirmBgColor:(UIColor *)confirmBgColor{
  162. _confirmBgColor = confirmBgColor;
  163. self.confirmBtn.backgroundColor = confirmBgColor;
  164. self.singleConfirmBtn.backgroundColor = confirmBgColor;
  165. }
  166. - (void)setCancelTitleColor:(UIColor *)cancelTitleColor{
  167. _cancelTitleColor = cancelTitleColor;
  168. [self.cancelBtn setTitleColor:cancelTitleColor forState:UIControlStateNormal];
  169. }
  170. - (void)setConfirmTitleColor:(UIColor *)confirmTitleColor{
  171. _confirmTitleColor = confirmTitleColor;
  172. [self.confirmBtn setTitleColor:confirmTitleColor forState:UIControlStateNormal];
  173. [self.singleConfirmBtn setTitleColor:confirmTitleColor forState:UIControlStateNormal];
  174. }
  175. - (void)setCancelFont:(UIFont *)cancelFont{
  176. _cancelFont = cancelFont;
  177. self.cancelBtn.titleLabel.font = cancelFont;
  178. }
  179. - (void)setConfirmFont:(UIFont *)confirmFont{
  180. _confirmFont = confirmFont;
  181. self.confirmBtn.titleLabel.font = confirmFont;
  182. self.singleConfirmBtn.titleLabel.font = confirmFont;
  183. }
  184. - (void)setCancelTitle:(NSString *)cancelTitle{
  185. _cancelTitle = cancelTitle;
  186. [self.cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];
  187. }
  188. - (void)setConfirmTitle:(NSString *)confirmTitle{
  189. _confirmTitle = confirmTitle;
  190. [self.confirmBtn setTitle:confirmTitle forState:UIControlStateNormal];
  191. [self.singleConfirmBtn setTitle:confirmTitle forState:UIControlStateNormal];
  192. }
  193. - (void)setCancelRadius:(CGFloat)cancelRadius{
  194. _cancelRadius = cancelRadius;
  195. self.cancelBtn.layer.cornerRadius = cancelRadius;
  196. }
  197. - (void)setConfirmRadius:(CGFloat)confirmRadius{
  198. _confirmRadius = confirmRadius;
  199. self.confirmBtn.layer.cornerRadius = confirmRadius;
  200. self.singleConfirmBtn.layer.cornerRadius = confirmRadius;
  201. }
  202. - (void)setCancelBorderColor:(UIColor *)cancelBorderColor{
  203. _cancelBorderColor = cancelBorderColor;
  204. self.cancelBtn.layer.borderColor = cancelBorderColor.CGColor;
  205. }
  206. - (void)setConfirmBorderColor:(UIColor *)confirmBorderColor{
  207. _confirmBorderColor = confirmBorderColor;
  208. self.confirmBtn.layer.borderColor = confirmBorderColor.CGColor;
  209. self.singleConfirmBtn.layer.borderColor = confirmBorderColor.CGColor;
  210. }
  211. - (void)setCancelBorderWidth:(CGFloat)cancelBorderWidth{
  212. _cancelBorderWidth = cancelBorderWidth;
  213. self.cancelBtn.layer.borderWidth = cancelBorderWidth;
  214. }
  215. - (void)setConfirmBorderWidth:(CGFloat)confirmBorderWidth{
  216. _confirmBorderWidth = confirmBorderWidth;
  217. self.confirmBtn.layer.borderWidth = confirmBorderWidth;
  218. self.singleConfirmBtn.layer.borderWidth = confirmBorderWidth;
  219. }
  220. - (void)setMaxLength:(NSInteger)maxLength{
  221. _maxLength = maxLength;
  222. self.limitLb.text = stringFormat(@"0/%ld",maxLength);
  223. }
  224. - (void)setTextAlignment:(NSTextAlignment)textAlignment{
  225. _textAlignment = textAlignment;
  226. self.contentTextView.textAlignment = textAlignment;
  227. }
  228. - (void)setPlaceholderText:(NSString *)placeholderText{
  229. _placeholderText = placeholderText;
  230. self.contentTextView.placeholderText = placeholderText;
  231. }
  232. - (void)textViewDidChangeSelection:(YYTextView *)textView{
  233. NSString *string = textView.text;
  234. NSInteger maxLength = self.maxLength;
  235. //获取高亮部分
  236. YYTextRange *selectedRange = [textView valueForKey:@"_markedTextRange"];
  237. NSRange range = [selectedRange asRange];
  238. NSString *realString = [string substringWithRange:NSMakeRange(0, string.length - range.length)];
  239. if (realString.length >= maxLength){
  240. textView.text = [realString substringWithRange:NSMakeRange(0, maxLength)];
  241. }
  242. self.limitLb.text = stringFormat(@"%ld/%ld",realString.length,self.maxLength);
  243. }
  244. - (UILabel *)titleLb{
  245. if (!_titleLb) {
  246. _titleLb = [[UILabel alloc]init];
  247. _titleLb.font = self.titleFont;
  248. _titleLb.textColor = self.titleColor;
  249. _titleLb.textAlignment = NSTextAlignmentCenter;
  250. _titleLb.text = self.titleText;
  251. }
  252. return _titleLb;
  253. }
  254. - (YYTextView *)contentTextView{
  255. if (!_contentTextView) {
  256. _contentTextView = [[YYTextView alloc]init];
  257. _contentTextView.delegate = self;
  258. _contentTextView.font = LCFont(15);
  259. _contentTextView.textColor = HexColorFromRGB(0x000000);
  260. _contentTextView.placeholderText = @"请输入内容";
  261. _contentTextView.backgroundColor = HexColorFromRGB(0xF6F6F6);
  262. _contentTextView.layer.cornerRadius = adapt(10);
  263. }
  264. return _contentTextView;
  265. }
  266. - (UILabel *)limitLb{
  267. if (!_limitLb) {
  268. _limitLb = [[UILabel alloc]init];
  269. _limitLb.font = LCFont(11);
  270. _limitLb.textColor = HexColorFromRGB(0x333333);
  271. _limitLb.textAlignment = NSTextAlignmentRight;
  272. _limitLb.text = stringFormat(@"0/%ld",self.maxLength);
  273. }
  274. return _limitLb;
  275. }
  276. - (UIView *)buttonView{
  277. if (!_buttonView) {
  278. _buttonView = [[UIView alloc]init];
  279. }
  280. return _buttonView;
  281. }
  282. - (UIButton *)cancelBtn {
  283. if(!_cancelBtn){
  284. _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  285. _cancelBtn.backgroundColor = self.cancelBgColor;
  286. _cancelBtn.titleLabel.font = self.cancelFont;
  287. [_cancelBtn setTitleColor:self.cancelTitleColor forState:UIControlStateNormal];
  288. [_cancelBtn setTitle:self.cancelTitle forState:UIControlStateNormal];
  289. _cancelBtn.layer.cornerRadius = self.cancelRadius;
  290. _cancelBtn.layer.borderWidth = self.cancelBorderWidth;
  291. _cancelBtn.layer.borderColor = self.cancelBorderColor.CGColor;
  292. WS(weakSelf)
  293. [[[_cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  294. if (weakSelf.buttonBlock) {
  295. weakSelf.buttonBlock(NO,@"");
  296. }
  297. }];
  298. }
  299. return _cancelBtn;
  300. }
  301. - (UIButton *)confirmBtn {
  302. if(!_confirmBtn){
  303. _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  304. _confirmBtn.backgroundColor = self.confirmBgColor;
  305. _confirmBtn.titleLabel.font = self.confirmFont;
  306. [_confirmBtn setTitleColor:self.confirmTitleColor forState:UIControlStateNormal];
  307. [_confirmBtn setTitle:self.confirmTitle forState:UIControlStateNormal];
  308. _confirmBtn.layer.cornerRadius = self.confirmRadius;
  309. _confirmBtn.layer.borderWidth = self.confirmBorderWidth;
  310. _confirmBtn.layer.borderColor = self.confirmBorderColor.CGColor;
  311. WS(weakSelf)
  312. [[[_confirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  313. if (weakSelf.buttonBlock) {
  314. weakSelf.buttonBlock(YES,weakSelf.contentTextView.text);
  315. }
  316. }];
  317. }
  318. return _confirmBtn;
  319. }
  320. - (UIButton *)singleConfirmBtn {
  321. if(!_singleConfirmBtn){
  322. _singleConfirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  323. _singleConfirmBtn.backgroundColor = self.confirmBgColor;
  324. _singleConfirmBtn.titleLabel.font = self.confirmFont;
  325. [_singleConfirmBtn setTitleColor:self.confirmTitleColor forState:UIControlStateNormal];
  326. [_singleConfirmBtn setTitle:self.confirmTitle forState:UIControlStateNormal];
  327. _singleConfirmBtn.layer.cornerRadius = self.confirmRadius;
  328. _singleConfirmBtn.layer.borderWidth = self.confirmBorderWidth;
  329. _singleConfirmBtn.layer.borderColor = self.confirmBorderColor.CGColor;
  330. WS(weakSelf)
  331. [[[_singleConfirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  332. if (weakSelf.buttonBlock) {
  333. weakSelf.buttonBlock(YES,weakSelf.contentTextView.text);
  334. }
  335. }];
  336. }
  337. return _singleConfirmBtn;
  338. }
  339. @end