Nessuna descrizione

UIPlaceHolderTextView.m 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // UIPlaceHolderTextView.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 16/6/13.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "UIPlaceHolderTextView.h"
  9. @interface UIPlaceHolderTextView ()
  10. @property (nonatomic, retain) UILabel *placeHolderLabel;
  11. @end
  12. @implementation UIPlaceHolderTextView
  13. CGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25;
  14. - (void)dealloc
  15. {
  16. [[NSNotificationCenter defaultCenter] removeObserver:self];
  17. #if __has_feature(objc_arc)
  18. #else
  19. [_placeHolderLabel release]; _placeHolderLabel = nil;
  20. [_placeholderColor release]; _placeholderColor = nil;
  21. [_placeholder release]; _placeholder = nil;
  22. [super dealloc];
  23. #endif
  24. }
  25. - (void)awakeFromNib
  26. {
  27. [super awakeFromNib];
  28. // Use Interface Builder User Defined Runtime Attributes to set
  29. // placeholder and placeholderColor in Interface Builder.
  30. if (!self.placeholder) {
  31. [self setPlaceholder:@""];
  32. }
  33. if (!self.placeholderColor) {
  34. [self setPlaceholderColor:[UIColor lightGrayColor]];
  35. }
  36. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
  37. }
  38. - (id)initWithFrame:(CGRect)frame
  39. {
  40. if( (self = [super initWithFrame:frame]) )
  41. {
  42. [self setPlaceholder:@""];
  43. [self setPlaceholderColor:[UIColor lightGrayColor]];
  44. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
  45. }
  46. return self;
  47. }
  48. - (void)textChanged:(NSNotification *)notification
  49. {
  50. if([[self placeholder] length] == 0)
  51. {
  52. return;
  53. }
  54. [UIView animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION animations:^{
  55. if([[self text] length] == 0)
  56. {
  57. [[self viewWithTag:999] setAlpha:1];
  58. }
  59. else
  60. {
  61. [[self viewWithTag:999] setAlpha:0];
  62. }
  63. }];
  64. }
  65. - (void)setText:(NSString *)text {
  66. [super setText:text];
  67. [self textChanged:nil];
  68. }
  69. - (void)drawRect:(CGRect)rect
  70. {
  71. if( [[self placeholder] length] > 0 )
  72. {
  73. if (_placeHolderLabel == nil )
  74. {
  75. _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];
  76. _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
  77. _placeHolderLabel.numberOfLines = 0;
  78. _placeHolderLabel.font = self.font;
  79. _placeHolderLabel.backgroundColor = [UIColor clearColor];
  80. _placeHolderLabel.textColor = self.placeholderColor;
  81. _placeHolderLabel.alpha = 0;
  82. _placeHolderLabel.tag = 999;
  83. [self addSubview:_placeHolderLabel];
  84. }
  85. _placeHolderLabel.text = self.placeholder;
  86. [_placeHolderLabel sizeToFit];
  87. [self sendSubviewToBack:_placeHolderLabel];
  88. }
  89. if( [[self text] length] == 0 && [[self placeholder] length] > 0 )
  90. {
  91. [[self viewWithTag:999] setAlpha:1];
  92. }
  93. [super drawRect:rect];
  94. }
  95. @end