UIPlaceHolderTextView.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // UIPlaceHolderTextView.m
  3. // xbxm
  4. //
  5. // Created by Scott Su on 14-12-6.
  6. // Copyright (c) 2014年 tongxueplus. All rights reserved.
  7. //
  8. #import "UIPlaceHolderTextView.h"
  9. @interface UIPlaceHolderTextView ()
  10. @end
  11. @implementation UIPlaceHolderTextView
  12. CGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25;
  13. - (instancetype)init{
  14. self = [super init];
  15. if (self) {
  16. if (!self.placeholder) {
  17. [self setPlaceholder:@""];
  18. }
  19. if (!self.placeholderColor) {
  20. [self setPlaceholderColor:UIColorHex(#999999)];
  21. }
  22. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
  23. if (_placeHolderLabel == nil )
  24. {
  25. _placeHolderLabel = [[UILabel alloc] init];
  26. _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
  27. _placeHolderLabel.numberOfLines = 0;
  28. _placeHolderLabel.font = self.font;
  29. _placeHolderLabel.backgroundColor = [UIColor clearColor];
  30. _placeHolderLabel.textColor = self.placeholderColor;
  31. _placeHolderLabel.alpha = 0;
  32. _placeHolderLabel.tag = 999;
  33. [self addSubview:_placeHolderLabel];
  34. }
  35. _placeHolderLabel.text = self.placeholder;
  36. [_placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.left.top.mas_offset(8);
  38. make.right.mas_offset(-8);
  39. make.height.mas_greaterThanOrEqualTo(20);
  40. }];
  41. [self sendSubviewToBack:_placeHolderLabel];
  42. }
  43. return self;
  44. }
  45. - (void)awakeFromNib
  46. {
  47. [super awakeFromNib];
  48. // Use Interface Builder User Defined Runtime Attributes to set
  49. // placeholder and placeholderColor in Interface Builder.
  50. if (!self.placeholder) {
  51. [self setPlaceholder:@""];
  52. }
  53. if (!self.placeholderColor) {
  54. [self setPlaceholderColor:UIColorHex(#999999)];
  55. }
  56. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
  57. if (_placeHolderLabel == nil )
  58. {
  59. // _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];
  60. _placeHolderLabel = [[UILabel alloc] init];
  61. _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
  62. _placeHolderLabel.numberOfLines = 0;
  63. _placeHolderLabel.font = self.font;
  64. _placeHolderLabel.backgroundColor = [UIColor clearColor];
  65. _placeHolderLabel.textColor = self.placeholderColor;
  66. _placeHolderLabel.alpha = 0;
  67. _placeHolderLabel.tag = 999;
  68. // _placeHolderLabel.backgroundColor = RGB(51, 51, 51);
  69. [self addSubview:_placeHolderLabel];
  70. }
  71. _placeHolderLabel.text = self.placeholder;
  72. // [_placeHolderLabel sizeToFit];
  73. // CGFloat H = [self.placeholder sizeWithFont:self.font byWidth:self.bounds.size.width - 16].height;
  74. [_placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  75. make.left.top.mas_offset(8);
  76. make.right.mas_offset(-8);
  77. make.height.mas_greaterThanOrEqualTo(20);
  78. }];
  79. // _placeHolderLabel.frame = CGRectMake(8, 8, self.bounds.size.width - 16, [self.placeholder sizeWithFont:self.font byWidth:self.bounds.size.width - 16].height);
  80. [self sendSubviewToBack:_placeHolderLabel];
  81. }
  82. - (void)setPlaceholder:(NSString *)placeholder
  83. {
  84. _placeholder = placeholder;
  85. _placeHolderLabel.text = _placeholder;
  86. }
  87. - (id)initWithFrame:(CGRect)frame
  88. {
  89. if( (self = [super initWithFrame:frame]) )
  90. {
  91. [self setPlaceholder:@""];
  92. [self setPlaceholderColor:[UIColor lightGrayColor]];
  93. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
  94. }
  95. return self;
  96. }
  97. - (void)dealloc
  98. {
  99. [[NSNotificationCenter defaultCenter] removeObserver:self];
  100. }
  101. - (void)textChanged:(NSNotification *)notification
  102. {
  103. if([[self placeholder] length] == 0)
  104. {
  105. return;
  106. }
  107. [self.placeHolderLabel setText:self.placeholder];
  108. [UIView animateWithDuration:0.2 animations:^{
  109. if([[self text] length] == 0)
  110. {
  111. [(UILabel *)[self viewWithTag:999] setAlpha:1];
  112. }
  113. else
  114. {
  115. [(UILabel *)[self viewWithTag:999] setAlpha:0];
  116. }
  117. }];
  118. }
  119. - (void)setText:(NSString *)text {
  120. [super setText:text];
  121. [self textChanged:nil];
  122. }
  123. - (void)drawRect:(CGRect)rect
  124. {
  125. if( [[self placeholder] length] > 0 )
  126. {
  127. }
  128. if( [[self text] length] == 0 && [[self placeholder] length] > 0 )
  129. {
  130. [(UILabel *)[self viewWithTag:999] setAlpha:1];
  131. }
  132. [super drawRect:rect];
  133. }
  134. @end