WXHTextBox.m 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // WXHTextBox.m
  3. // xh_textfield
  4. //
  5. // Created by 路 on 2019/4/12.
  6. // Copyright © 2019年 路. All rights reserved.
  7. //
  8. #import "WXHTextBox.h"
  9. @implementation WXHTextBox {
  10. NSTimer *_timer;
  11. UIView *_twinkle_line;
  12. }
  13. - (id)init {
  14. self = [super init];
  15. if (self) {
  16. // _normalColor = [UIColor grayColor];
  17. // _activeColor = [UIColor orangeColor];
  18. self.layer.borderWidth = 1.f;
  19. self.layer.cornerRadius = 3.f;
  20. self.layer.masksToBounds = YES;
  21. _twinkle_line = [[UIView alloc] init];
  22. _twinkle_line.backgroundColor = _activeColor;
  23. _twinkle_line.hidden = YES;
  24. [self addSubview:_twinkle_line];
  25. _label = [[UILabel alloc] init];
  26. _label.font = [UIFont systemFontOfSize:15];
  27. _label.textAlignment = NSTextAlignmentCenter;
  28. [self addSubview:_label];
  29. } return self;
  30. }
  31. - (void)layoutSubviews {
  32. [super layoutSubviews];
  33. _twinkle_line.bounds = CGRectMake(0, 0, 2.f, 15.f);
  34. _twinkle_line.center = CGPointMake(self.frame.size.width * .5, self.frame.size.height * .5);
  35. _label.frame = self.bounds;
  36. }
  37. - (void)update:(NSTimer *)sender {
  38. if (_active) {
  39. _twinkle_line.hidden = !_twinkle_line.hidden;
  40. }
  41. }
  42. - (void)setActive:(BOOL)active {
  43. _active = active;
  44. if (_active) {
  45. self.layer.borderColor = _activeColor.CGColor;
  46. _label.textColor = _activeTextColor;
  47. if (!_timer) {
  48. _timer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(update:) userInfo:nil repeats:YES];
  49. [_timer fire];
  50. }
  51. } else {
  52. self.layer.borderColor = _normalColor.CGColor;
  53. _label.textColor = _normalTextColor;
  54. [self destroy];
  55. _twinkle_line.hidden = YES;
  56. }
  57. }
  58. - (void)destroy; {
  59. [_timer invalidate];
  60. _timer = nil;
  61. }
  62. @end