YOUPAINIMGrowingInternalTextView.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // YOUPAINIMGrowingInternalTextView.m
  3. // NIMKit
  4. //
  5. // Created by chris on 16/3/27.
  6. // Copyright © 2016年 Netease. All rights reserved.
  7. //
  8. #import "YOUPAINIMGrowingInternalTextView.h"
  9. @interface YOUPAINIMGrowingInternalTextView()
  10. @property (nonatomic,assign) BOOL displayPlaceholder;
  11. @end
  12. @implementation YOUPAINIMGrowingInternalTextView
  13. - (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer
  14. {
  15. self = [super initWithFrame:frame textContainer:textContainer];
  16. if (self) {
  17. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChangeNotification:) name:UITextViewTextDidChangeNotification object:self];
  18. }
  19. return self;
  20. }
  21. - (void)dealloc
  22. {
  23. [[NSNotificationCenter defaultCenter] removeObserver:self];
  24. }
  25. - (void)setText:(NSString *)text
  26. {
  27. [super setText:text];
  28. [self updatePlaceholder];
  29. }
  30. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
  31. {
  32. if(action ==@selector(copy:) ||
  33. action ==@selector(selectAll:)||
  34. action ==@selector(cut:)||
  35. action ==@selector(select:)||
  36. action ==@selector(paste:)) {
  37. return[super canPerformAction:action withSender:sender];
  38. }
  39. return NO;
  40. }
  41. - (void)setPlaceholderAttributedText:(NSAttributedString *)placeholderAttributedText
  42. {
  43. _placeholderAttributedText = placeholderAttributedText;
  44. [self setNeedsDisplay];
  45. }
  46. - (void)layoutSubviews
  47. {
  48. [super layoutSubviews];
  49. [self setNeedsDisplay];
  50. }
  51. #pragma mark - Private
  52. - (void)setDisplayPlaceholder:(BOOL)displayPlaceholder
  53. {
  54. BOOL oldValue = _displayPlaceholder;
  55. _displayPlaceholder = displayPlaceholder;
  56. if (oldValue != self.displayPlaceholder) {
  57. [self setNeedsDisplay];
  58. }
  59. }
  60. - (void)updatePlaceholder
  61. {
  62. self.displayPlaceholder = self.text.length == 0;
  63. }
  64. - (void)textDidChangeNotification:(NSNotification *)notification
  65. {
  66. [self updatePlaceholder];
  67. }
  68. - (void)drawRect:(CGRect)rect
  69. {
  70. [super drawRect:rect];
  71. if (!self.displayPlaceholder) {
  72. return;
  73. }
  74. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  75. paragraphStyle.alignment = self.textAlignment;
  76. CGRect targetRect = CGRectMake(5, 8 + self.contentInset.top, self.frame.size.width - self.contentInset.left, self.frame.size.height - self.contentInset.top);
  77. NSAttributedString *attributedString = self.placeholderAttributedText;
  78. [attributedString drawInRect:targetRect];
  79. }
  80. @end