YJJTextField.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // YJJTextField.m
  3. // YJJTextField
  4. //
  5. // Created by arges on 2017/6/5.
  6. // Copyright © 2017年 ArgesYao. All rights reserved.
  7. //
  8. #import "YJJTextField.h"
  9. #define YJJ_Color(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
  10. const static CGFloat margin = 10.0;
  11. const static CGFloat tableViewH = 80.0;
  12. const static CGFloat animateDuration = 0.5;
  13. @interface YJJTextField ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>
  14. /** 上浮的占位文本 */
  15. @property (weak, nonatomic) IBOutlet UILabel *placeHolderLabel;
  16. /** 左侧小图标 */
  17. @property (weak, nonatomic) IBOutlet UIImageView *leftImageView;
  18. /** 字数限制文本 */
  19. @property (weak, nonatomic) IBOutlet UILabel *textLengthLabel;
  20. /** 底部线条 */
  21. @property (weak, nonatomic) IBOutlet UIView *bottomLine;
  22. /** 错误提示文本 */
  23. @property (weak, nonatomic) IBOutlet UILabel *errorLabel;
  24. /** 窗口 */
  25. @property (nonatomic,strong) UIWindow *window;
  26. /** 表格视图 */
  27. @property (nonatomic,strong) UITableView *tableView;
  28. /** 历史数据 */
  29. @property (nonatomic,strong) NSArray *historyContentArr;
  30. @end
  31. @implementation YJJTextField
  32. #pragma mark - 懒加载
  33. - (UITableView *)tableView
  34. {
  35. if (!_tableView) {
  36. _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
  37. _tableView.dataSource = self;
  38. _tableView.delegate = self;
  39. _tableView.alpha = 0.0;
  40. _tableView.layer.borderWidth = 1.0;
  41. _tableView.layer.borderColor = YJJ_Color(233, 233, 233).CGColor;
  42. }
  43. return _tableView;
  44. }
  45. #pragma mark - 初始化
  46. - (void)awakeFromNib
  47. {
  48. [super awakeFromNib];
  49. self.textField.delegate = self;
  50. [self.textField addTarget:self action:@selector(searchTextFieldChange:) forControlEvents:UIControlEventEditingChanged];
  51. self.textColor = YJJ_Color(85, 85, 85);
  52. self.textLengthLabelColor = YJJ_Color(92, 94, 102);
  53. self.placeHolderLabelColor = YJJ_Color(1, 183, 164);
  54. self.lineDefaultColor = YJJ_Color(220, 220, 220);
  55. self.lineSelectedColor = YJJ_Color(1, 183, 164);
  56. self.lineWarningColor = YJJ_Color(252, 57, 24);
  57. }
  58. #pragma mark - 公共方法
  59. + (instancetype)yjj_textField
  60. {
  61. return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]firstObject];
  62. }
  63. - (void)setPlaceHolderLabelHidden:(BOOL)isHidden
  64. {
  65. if (isHidden) {
  66. [UIView animateWithDuration:animateDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  67. self.placeHolderLabel.alpha = 0.0f;
  68. self.textField.placeholder = self.placeholder;
  69. self.bottomLine.backgroundColor = self.lineDefaultColor;
  70. } completion:nil];
  71. }else{
  72. [UIView animateWithDuration:animateDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  73. self.placeHolderLabel.alpha = 1.0f;
  74. self.placeHolderLabel.text = self.placeholder;
  75. self.textField.placeholder = @"";
  76. self.bottomLine.backgroundColor = self.lineSelectedColor;
  77. } completion:nil];
  78. }
  79. }
  80. #pragma mark - 提示列表
  81. - (void)showTheHistoryContentTableView:(CGFloat)y
  82. {
  83. self.window = [UIApplication sharedApplication].keyWindow;
  84. self.window.backgroundColor = [UIColor clearColor];
  85. [self.window addSubview:self.tableView];
  86. self.tableView.frame = CGRectMake(margin, y, self.frame.size.width-margin*2, tableViewH);
  87. [UIView animateWithDuration:animateDuration animations:^{
  88. self.tableView.alpha = 1.0;
  89. }];
  90. }
  91. - (void)dismissTheHistoryContentTableView
  92. {
  93. [UIView animateWithDuration:animateDuration animations:^{
  94. self.tableView.alpha = 0.0;
  95. } completion:^(BOOL finished) {
  96. [self.tableView removeFromSuperview];
  97. self.window = nil;
  98. }];
  99. }
  100. - (void)loadHistoryContentWithKey:(NSString *)key
  101. {
  102. self.historyContentArr = nil;
  103. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  104. NSDictionary *dic = [defaults objectForKey:@"historyContent"];
  105. for (NSString *string in dic.allKeys) {
  106. if ([string isEqualToString:key]) {
  107. self.historyContentArr = dic[string];
  108. break;
  109. }
  110. }
  111. }
  112. #pragma mark - UITextFieldDelegate
  113. - (IBAction)textFieldEditingChanged:(UITextField *)sender
  114. {
  115. if (sender.text.length > self.maxLength) {
  116. [UIView animateWithDuration:animateDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  117. self.errorLabel.alpha = 1.0;
  118. self.errorLabel.textColor = self.lineWarningColor;
  119. self.bottomLine.backgroundColor = self.lineWarningColor;
  120. self.textLengthLabel.textColor = self.lineWarningColor;
  121. self.textField.textColor = self.lineWarningColor;
  122. //self.placeHolderLabel.textColor = self.lineWarningColor;
  123. } completion:nil];
  124. }else{
  125. [UIView animateWithDuration:animateDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  126. self.errorLabel.alpha = 0.0;
  127. self.bottomLine.backgroundColor = self.lineSelectedColor;
  128. self.textLengthLabel.textColor = self.textLengthLabelColor;
  129. self.textField.textColor = self.textColor;
  130. //self.placeHolderLabel.textColor = self.placeHolderLabelColor;
  131. } completion:nil];
  132. }
  133. self.textLengthLabel.text = [NSString stringWithFormat:@"%zd/%zd",sender.text.length,self.maxLength];
  134. }
  135. - (void)textFieldDidBeginEditing:(UITextField *)textField
  136. {
  137. [self setPlaceHolderLabelHidden:NO];
  138. [self loadHistoryContentWithKey:self.historyContentKey];
  139. if (self.historyContentArr.count != 0) {
  140. CGFloat y = CGRectGetMaxY(self.frame);
  141. [self showTheHistoryContentTableView:y];
  142. }
  143. }
  144. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  145. {
  146. [self endEditing:YES];
  147. [self setPlaceHolderLabelHidden:YES];
  148. [self dismissTheHistoryContentTableView];
  149. return YES;
  150. }
  151. // 去掉空格
  152. - (void)searchTextFieldChange:(UITextField *)textField{
  153. textField.text =[textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
  154. }
  155. #pragma mark - UITableViewDataSource
  156. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  157. {
  158. return self.historyContentArr.count;
  159. }
  160. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  161. {
  162. static NSString *ID = @"history";
  163. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  164. if (cell == nil) {
  165. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
  166. cell.textLabel.textColor = [UIColor darkGrayColor];
  167. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  168. }
  169. cell.textLabel.text = self.historyContentArr[indexPath.row];
  170. return cell;
  171. }
  172. #pragma mark - UITableViewDelegate
  173. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  174. {
  175. self.textField.text = self.historyContentArr[indexPath.row];
  176. self.textLengthLabel.text = [NSString stringWithFormat:@"%zd/%zd",self.textField.text.length,self.maxLength];
  177. [self dismissTheHistoryContentTableView];
  178. }
  179. #pragma mark - setter
  180. - (void)setMaxLength:(NSInteger)maxLength
  181. {
  182. _maxLength = maxLength;
  183. self.textLengthLabel.text = [NSString stringWithFormat:@"0/%zd",maxLength];
  184. }
  185. - (void)setErrorStr:(NSString *)errorStr
  186. {
  187. _errorStr = errorStr;
  188. self.errorLabel.text = errorStr;
  189. }
  190. - (void)setPlaceholder:(NSString *)placeholder
  191. {
  192. _placeholder = placeholder;
  193. self.textField.placeholder = placeholder;
  194. self.placeHolderLabel.text = placeholder;
  195. }
  196. - (void)setHistoryContentKey:(NSString *)historyContentKey
  197. {
  198. _historyContentKey = historyContentKey;
  199. }
  200. - (void)setLeftImageName:(NSString *)leftImageName
  201. {
  202. _leftImageName = leftImageName;
  203. self.leftImageView.image = [UIImage imageNamed:leftImageName];
  204. }
  205. - (void)setTextFont:(CGFloat)textFont
  206. {
  207. _textFont = textFont;
  208. self.textField.font = [UIFont systemFontOfSize:textFont];
  209. }
  210. - (void)setTextColor:(UIColor *)textColor
  211. {
  212. _textColor = textColor;
  213. self.textField.textColor = textColor;
  214. self.tintColor = textColor; // 光标颜色
  215. }
  216. - (void)setPlaceHolderFont:(CGFloat)placeHolderFont
  217. {
  218. _placeHolderFont = placeHolderFont;
  219. [self.textField setValue:[UIFont systemFontOfSize:placeHolderFont] forKeyPath:@"_placeholderLabel.font"];
  220. }
  221. - (void)setPlaceHolderColor:(UIColor *)placeHolderColor
  222. {
  223. _placeHolderColor = placeHolderColor;
  224. [self.textField setValue:placeHolderColor forKeyPath:@"_placeholderLabel.textColor"];
  225. }
  226. - (void)setTextLengthLabelColor:(UIColor *)textLengthLabelColor
  227. {
  228. _textLengthLabelColor = textLengthLabelColor;
  229. self.textLengthLabel.textColor = textLengthLabelColor;
  230. }
  231. - (void)setPlaceHolderLabelColor:(UIColor *)placeHolderLabelColor
  232. {
  233. _placeHolderLabelColor = placeHolderLabelColor;
  234. self.placeHolderLabel.textColor = placeHolderLabelColor;
  235. }
  236. - (void)setLineDefaultColor:(UIColor *)lineDefaultColor
  237. {
  238. _lineDefaultColor = lineDefaultColor;
  239. self.bottomLine.backgroundColor = lineDefaultColor;
  240. }
  241. - (void)setLineSelectedColor:(UIColor *)lineSelectedColor
  242. {
  243. _lineSelectedColor = lineSelectedColor;
  244. }
  245. - (void)setLineWarningColor:(UIColor *)lineWarningColor
  246. {
  247. _lineWarningColor = lineWarningColor;
  248. }
  249. @end