1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /************************************************************
- * * Hyphenate CONFIDENTIAL
- * __________________
- * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
- *
- * NOTICE: All information contained herein is, and remains
- * the property of Hyphenate Inc.
- * Dissemination of this information or reproduction of this material
- * is strictly forbidden unless prior written permission is obtained
- * from Hyphenate Inc.
- */
- #import "EMTextView.h"
- @implementation EMTextView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- _contentColor = [UIColor blackColor];
- _placeholderColor = [UIColor lightGrayColor];
- _editing = NO;
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startEditing:) name:UITextViewTextDidBeginEditingNotification object:self];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishEditing:) name:UITextViewTextDidEndEditingNotification object:self];
- }
- return self;
- }
- #pragma mark - super
- - (void)setTextColor:(UIColor *)textColor
- {
- [super setTextColor:textColor];
-
- _contentColor = textColor;
- }
- - (NSString *)text
- {
- if ([super.text isEqualToString:_placeholder] && super.textColor == _placeholderColor) {
- return @"";
- }
-
- return [super text];
- }
- - (void)setText:(NSString *)string
- {
- if ([self.text length] == 0) {
- super.textColor = _contentColor;
- }
- super.text = string;
- }
- #pragma mark - setting
- - (void)setPlaceholder:(NSString *)string
- {
- _placeholder = string;
-
- [self finishEditing:nil];
- }
- - (void)setPlaceholderColor:(UIColor *)color
- {
- _placeholderColor = color;
- }
- #pragma mark - notification
- - (void)startEditing:(NSNotification *)notification
- {
- _editing = YES;
-
- if ([super.text isEqualToString:_placeholder] && super.textColor == _placeholderColor) {
- super.textColor = _contentColor;
- super.text = @"";
- }
- }
- - (void)finishEditing:(NSNotification *)notification
- {
- _editing = NO;
-
- if (super.text.length == 0) {
- super.textColor = _placeholderColor;
- super.text = _placeholder;
- }
- else{
- super.textColor = _contentColor;
- }
- }
- @end
|