123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- //
- // YOUPAINIMGrowingInternalTextView.m
- // NIMKit
- //
- // Created by chris on 16/3/27.
- // Copyright © 2016年 Netease. All rights reserved.
- //
- #import "YOUPAINIMGrowingInternalTextView.h"
- @interface YOUPAINIMGrowingInternalTextView()
- @property (nonatomic,assign) BOOL displayPlaceholder;
- @end
- @implementation YOUPAINIMGrowingInternalTextView
- - (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer
- {
- self = [super initWithFrame:frame textContainer:textContainer];
- if (self) {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChangeNotification:) name:UITextViewTextDidChangeNotification object:self];
- }
- return self;
- }
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)setText:(NSString *)text
- {
- [super setText:text];
- [self updatePlaceholder];
- }
- - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
- {
- if(action ==@selector(copy:) ||
-
- action ==@selector(selectAll:)||
-
- action ==@selector(cut:)||
-
- action ==@selector(select:)||
-
- action ==@selector(paste:)) {
-
- return[super canPerformAction:action withSender:sender];
- }
- return NO;
- }
- - (void)setPlaceholderAttributedText:(NSAttributedString *)placeholderAttributedText
- {
- _placeholderAttributedText = placeholderAttributedText;
- [self setNeedsDisplay];
- }
- - (void)layoutSubviews
- {
- [super layoutSubviews];
- [self setNeedsDisplay];
- }
- #pragma mark - Private
- - (void)setDisplayPlaceholder:(BOOL)displayPlaceholder
- {
- BOOL oldValue = _displayPlaceholder;
- _displayPlaceholder = displayPlaceholder;
- if (oldValue != self.displayPlaceholder) {
- [self setNeedsDisplay];
- }
- }
- - (void)updatePlaceholder
- {
- self.displayPlaceholder = self.text.length == 0;
- }
- - (void)textDidChangeNotification:(NSNotification *)notification
- {
- [self updatePlaceholder];
- }
- - (void)drawRect:(CGRect)rect
- {
- [super drawRect:rect];
- if (!self.displayPlaceholder) {
- return;
- }
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- paragraphStyle.alignment = self.textAlignment;
-
- CGRect targetRect = CGRectMake(5, 8 + self.contentInset.top, self.frame.size.width - self.contentInset.left, self.frame.size.height - self.contentInset.top);
-
- NSAttributedString *attributedString = self.placeholderAttributedText;
- [attributedString drawInRect:targetRect];
- }
- @end
|