EaseTextView.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "EaseTextView.h"
  13. @implementation EaseTextView
  14. #pragma mark - Setters
  15. - (void)setPlaceHolder:(NSString *)placeHolder {
  16. if([placeHolder isEqualToString:_placeHolder]) {
  17. return;
  18. }
  19. NSUInteger maxChars = [EaseTextView maxCharactersPerLine];
  20. if([placeHolder length] > maxChars) {
  21. placeHolder = [placeHolder substringToIndex:maxChars - 8];
  22. placeHolder = [[placeHolder stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByAppendingFormat:@"..."];
  23. }
  24. _placeHolder = placeHolder;
  25. [self setNeedsDisplay];
  26. }
  27. - (void)setPlaceHolderTextColor:(UIColor *)placeHolderTextColor {
  28. if([placeHolderTextColor isEqual:_placeHolderTextColor]) {
  29. return;
  30. }
  31. _placeHolderTextColor = placeHolderTextColor;
  32. [self setNeedsDisplay];
  33. }
  34. #pragma mark - Message text view
  35. - (NSUInteger)numberOfLinesOfText
  36. {
  37. return [EaseTextView numberOfLinesForMessage:self.text];
  38. }
  39. + (NSUInteger)maxCharactersPerLine {
  40. return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) ? 33 : 109;
  41. }
  42. + (NSUInteger)numberOfLinesForMessage:(NSString *)text {
  43. return (text.length / [EaseTextView maxCharactersPerLine]) + 1;
  44. }
  45. #pragma mark - Text view overrides
  46. - (void)setText:(NSString *)text {
  47. [super setText:text];
  48. [self setNeedsDisplay];
  49. }
  50. - (void)setAttributedText:(NSAttributedString *)attributedText {
  51. [super setAttributedText:attributedText];
  52. [self setNeedsDisplay];
  53. }
  54. //- (void)setContentInset:(UIEdgeInsets)contentInset {
  55. // [super setContentInset:contentInset];
  56. // [self setNeedsDisplay];
  57. //}
  58. - (void)setFont:(UIFont *)font {
  59. [super setFont:font];
  60. [self setNeedsDisplay];
  61. }
  62. - (void)setTextAlignment:(NSTextAlignment)textAlignment {
  63. [super setTextAlignment:textAlignment];
  64. [self setNeedsDisplay];
  65. }
  66. #pragma mark - Notifications
  67. - (void)didReceiveTextDidChangeNotification:(NSNotification *)notification {
  68. [self setNeedsDisplay];
  69. }
  70. #pragma mark - Life cycle
  71. - (void)setup {
  72. self.accessibilityIdentifier = @"text_view";
  73. [[NSNotificationCenter defaultCenter] addObserver:self
  74. selector:@selector(didReceiveTextDidChangeNotification:)
  75. name:UITextViewTextDidChangeNotification
  76. object:self];
  77. _placeHolderTextColor = [UIColor lightGrayColor];
  78. self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  79. self.scrollIndicatorInsets = UIEdgeInsetsMake(10.0f, 0.0f, 10.0f, 8.0f);
  80. self.contentInset = UIEdgeInsetsZero;
  81. self.scrollEnabled = YES;
  82. self.scrollsToTop = NO;
  83. self.userInteractionEnabled = YES;
  84. self.font = [UIFont systemFontOfSize:16.0f];
  85. self.textColor = [UIColor blackColor];
  86. self.backgroundColor = [UIColor whiteColor];
  87. self.keyboardAppearance = UIKeyboardAppearanceDefault;
  88. self.keyboardType = UIKeyboardTypeDefault;
  89. self.returnKeyType = UIReturnKeyDefault;
  90. self.textAlignment = NSTextAlignmentLeft;
  91. }
  92. - (id)initWithFrame:(CGRect)frame
  93. {
  94. self = [super initWithFrame:frame];
  95. if (self) {
  96. // Initialization code
  97. [self setup];
  98. }
  99. return self;
  100. }
  101. - (void)dealloc {
  102. _placeHolder = nil;
  103. _placeHolderTextColor = nil;
  104. [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self];
  105. }
  106. #pragma mark - Drawing
  107. - (void)drawRect:(CGRect)rect
  108. {
  109. [super drawRect:rect];
  110. if([self.text length] == 0 && self.placeHolder) {
  111. CGRect placeHolderRect = CGRectMake(10.0f,
  112. 7.0f,
  113. rect.size.width,
  114. rect.size.height);
  115. [self.placeHolderTextColor set];
  116. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_0) {
  117. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  118. paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
  119. paragraphStyle.alignment = self.textAlignment;
  120. [self.placeHolder drawInRect:placeHolderRect
  121. withAttributes:@{ NSFontAttributeName : self.font,
  122. NSForegroundColorAttributeName : self.placeHolderTextColor,
  123. NSParagraphStyleAttributeName : paragraphStyle }];
  124. }
  125. else {
  126. [self.placeHolder drawInRect:placeHolderRect
  127. withFont:self.font
  128. lineBreakMode:NSLineBreakByTruncatingTail
  129. alignment:self.textAlignment];
  130. }
  131. }
  132. }
  133. @end