NewTopicTextView.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // NewTopicTextView.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2019/12/17.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "NewTopicTextView.h"
  9. @interface NewTopicTextView ()
  10. @end
  11. @implementation NewTopicTextView
  12. - (void)awakeFromNib
  13. {
  14. [super awakeFromNib];
  15. [self setCusView];
  16. }
  17. - (instancetype)init
  18. {
  19. if (self = [super init]) {
  20. [self setCusView];
  21. }
  22. return self;
  23. }
  24. - (instancetype)initWithFrame:(CGRect)frame
  25. {
  26. if( (self = [super initWithFrame:frame]) )
  27. {
  28. [self setCusView];
  29. }
  30. return self;
  31. }
  32. - (void)setCusView
  33. {
  34. if (!self.placeholder) {
  35. [self setPlaceholder:@""];
  36. }
  37. if (!self.placeholderColor) {
  38. [self setPlaceholderColor:UIColorHex(#999999)];
  39. }
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
  41. if (_placeHolderLabel == nil )
  42. {
  43. _placeHolderLabel = [[UILabel alloc] init];
  44. _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
  45. _placeHolderLabel.numberOfLines = 0;
  46. _placeHolderLabel.font = [UIFont systemFontOfSize:16];
  47. _placeHolderLabel.backgroundColor = [UIColor clearColor];
  48. _placeHolderLabel.textColor = self.placeholderColor;
  49. _placeHolderLabel.alpha = 0;
  50. _placeHolderLabel.tag = 999;
  51. [self addSubview:_placeHolderLabel];
  52. }
  53. _placeHolderLabel.text = self.placeholder;
  54. [_placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  55. make.edges.mas_offset(UIEdgeInsetsMake(5, 5, 5, 5));
  56. }];
  57. [self sendSubviewToBack:_placeHolderLabel];
  58. }
  59. - (void)setPlaceholder:(NSString *)placeholder
  60. {
  61. _placeholder = placeholder;
  62. _placeHolderLabel.text = _placeholder;
  63. }
  64. - (void)dealloc
  65. {
  66. [[NSNotificationCenter defaultCenter] removeObserver:self];
  67. }
  68. - (void)textChanged:(NSNotification *)notification
  69. {
  70. if([[self placeholder] length] == 0)
  71. {
  72. return;
  73. }
  74. [self.placeHolderLabel setText:self.placeholder];
  75. [UIView animateWithDuration:0.2 animations:^{
  76. if([[self text] length] == 0)
  77. {
  78. [(UILabel *)[self viewWithTag:999] setAlpha:1];
  79. }
  80. else
  81. {
  82. [(UILabel *)[self viewWithTag:999] setAlpha:0];
  83. }
  84. }];
  85. }
  86. - (void)setText:(NSString *)text {
  87. [super setText:text];
  88. [self textChanged:nil];
  89. }
  90. - (void)drawRect:(CGRect)rect
  91. {
  92. if( [[self placeholder] length] > 0 )
  93. {
  94. }
  95. if( [[self text] length] == 0 && [[self placeholder] length] > 0 )
  96. {
  97. [(UILabel *)[self viewWithTag:999] setAlpha:1];
  98. }
  99. [super drawRect:rect];
  100. }
  101. @end