UITextView+NTES.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // UITextView+NTES.m
  3. // NIM
  4. //
  5. // Created by chris on 2018/3/20.
  6. // Copyright © 2018年 Netease. All rights reserved.
  7. //
  8. #import "UITextView+NTES.h"
  9. #import <objc/runtime.h>
  10. @implementation UITextView (NTES)
  11. #define UI_PLACEHOLDER_TEXT_COLOR [UIColor colorWithRed:170.0/255.0 green:170.0/255.0 blue:170.0/255.0 alpha:1.0]
  12. @dynamic placeholder;
  13. @dynamic placeholderLabel;
  14. @dynamic textValue;
  15. -(void)setTextValue:(NSString *)textValue
  16. {
  17. // Change the text of our UITextView, and check whether we need to display the placeholder.
  18. self.text = textValue;
  19. [self checkIfNeedToDisplayPlaceholder];
  20. }
  21. -(NSString*)textValue
  22. {
  23. return self.text;
  24. }
  25. -(void)checkIfNeedToDisplayPlaceholder
  26. {
  27. // If our UITextView is empty, display our Placeholder label (if we have one)
  28. if (self.placeholderLabel == nil)
  29. return;
  30. self.placeholderLabel.hidden = (![self.text isEqualToString:@""]);
  31. }
  32. -(void)onTap
  33. {
  34. // When the user taps in our UITextView, we'll see if we need to remove the placeholder text.
  35. [self checkIfNeedToDisplayPlaceholder];
  36. // Make the onscreen keyboard appear.
  37. [self becomeFirstResponder];
  38. }
  39. -(void)keyPressed:(NSNotification*)notification
  40. {
  41. // The user has just typed a character in our UITextView (or pressed the delete key).
  42. // Do we need to display our Placeholder label ?
  43. [self checkIfNeedToDisplayPlaceholder];
  44. }
  45. #pragma mark - Add a "placeHolder" string to the UITextView class
  46. NSString const *kKeyPlaceHolder = @"kKeyPlaceHolder";
  47. -(void)setPlaceholder:(NSString *)_placeholder
  48. {
  49. // Sets our "placeholder" text string, creates a new UILabel to contain it, and modifies our UITextView to cope with
  50. // showing/hiding the UILabel when needed.
  51. objc_setAssociatedObject(self, &kKeyPlaceHolder, (id)_placeholder, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  52. self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, 0, 0)];
  53. self.placeholderLabel.numberOfLines = 1;
  54. self.placeholderLabel.text = _placeholder;
  55. self.placeholderLabel.textColor = UI_PLACEHOLDER_TEXT_COLOR;
  56. self.placeholderLabel.backgroundColor = [UIColor clearColor];
  57. self.placeholderLabel.userInteractionEnabled = true;
  58. self.placeholderLabel.font = self.font;
  59. [self addSubview:self.placeholderLabel];
  60. [self.placeholderLabel sizeToFit];
  61. // Whenever the user taps within the UITextView, we'll give the textview the focus, and hide the placeholder if necessary.
  62. [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap)]];
  63. // Whenever the user types something in the UITextView, we'll see if we need to hide/show the placeholder label.
  64. [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(keyPressed:) name:UITextViewTextDidChangeNotification object:nil];
  65. [self checkIfNeedToDisplayPlaceholder];
  66. }
  67. -(NSString*)placeholder
  68. {
  69. // Returns our "placeholder" text string
  70. return objc_getAssociatedObject(self, &kKeyPlaceHolder);
  71. }
  72. #pragma mark - Add a "UILabel" to this UITextView class
  73. NSString const *kKeyLabel = @"kKeyLabel";
  74. -(void)setPlaceholderLabel:(UILabel *)placeholderLabel
  75. {
  76. // Stores our new UILabel (which contains our placeholder string)
  77. objc_setAssociatedObject(self, &kKeyLabel, (id)placeholderLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  78. [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(keyPressed:) name:UITextViewTextDidChangeNotification object:nil];
  79. [self checkIfNeedToDisplayPlaceholder];
  80. }
  81. -(UILabel*)placeholderLabel
  82. {
  83. // Returns our new UILabel
  84. return objc_getAssociatedObject(self, &kKeyLabel);
  85. }
  86. @end