EMTextView.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import "EMTextView.h"
  13. @implementation EMTextView
  14. - (id)initWithFrame:(CGRect)frame
  15. {
  16. self = [super initWithFrame:frame];
  17. if (self) {
  18. // Initialization code
  19. _contentColor = [UIColor blackColor];
  20. _placeholderColor = [UIColor lightGrayColor];
  21. _editing = NO;
  22. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startEditing:) name:UITextViewTextDidBeginEditingNotification object:self];
  23. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishEditing:) name:UITextViewTextDidEndEditingNotification object:self];
  24. }
  25. return self;
  26. }
  27. #pragma mark - super
  28. - (void)setTextColor:(UIColor *)textColor
  29. {
  30. [super setTextColor:textColor];
  31. _contentColor = textColor;
  32. }
  33. - (NSString *)text
  34. {
  35. if ([super.text isEqualToString:_placeholder] && super.textColor == _placeholderColor) {
  36. return @"";
  37. }
  38. return [super text];
  39. }
  40. - (void)setText:(NSString *)string
  41. {
  42. if ([self.text length] == 0) {
  43. super.textColor = _contentColor;
  44. }
  45. super.text = string;
  46. }
  47. #pragma mark - setting
  48. - (void)setPlaceholder:(NSString *)string
  49. {
  50. _placeholder = string;
  51. [self finishEditing:nil];
  52. }
  53. - (void)setPlaceholderColor:(UIColor *)color
  54. {
  55. _placeholderColor = color;
  56. }
  57. #pragma mark - notification
  58. - (void)startEditing:(NSNotification *)notification
  59. {
  60. _editing = YES;
  61. if ([super.text isEqualToString:_placeholder] && super.textColor == _placeholderColor) {
  62. super.textColor = _contentColor;
  63. super.text = @"";
  64. }
  65. }
  66. - (void)finishEditing:(NSNotification *)notification
  67. {
  68. _editing = NO;
  69. if (super.text.length == 0) {
  70. super.textColor = _placeholderColor;
  71. super.text = _placeholder;
  72. }
  73. else{
  74. super.textColor = _contentColor;
  75. }
  76. }
  77. @end