HPGrowingTextView.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. //
  2. // HPTextView.m
  3. //
  4. // Created by Hans Pinckaers on 29-06-10.
  5. //
  6. // MIT License
  7. //
  8. // Copyright (c) 2011 Hans Pinckaers
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. #import "HPGrowingTextView.h"
  28. #import "HPTextViewInternal.h"
  29. @interface HPGrowingTextView(private)
  30. -(void)commonInitialiser;
  31. -(void)resizeTextView:(NSInteger)newSizeH;
  32. -(void)growDidStop;
  33. @end
  34. @implementation HPGrowingTextView
  35. @synthesize internalTextView;
  36. @synthesize delegate;
  37. @synthesize maxHeight;
  38. @synthesize minHeight;
  39. @synthesize font;
  40. @synthesize textColor;
  41. @synthesize textAlignment;
  42. @synthesize selectedRange;
  43. @synthesize editable;
  44. @synthesize dataDetectorTypes;
  45. @synthesize animateHeightChange;
  46. @synthesize animationDuration;
  47. @synthesize returnKeyType;
  48. @dynamic placeholder;
  49. @dynamic placeholderColor;
  50. // having initwithcoder allows us to use HPGrowingTextView in a Nib. -- aob, 9/2011
  51. - (id)initWithCoder:(NSCoder *)aDecoder
  52. {
  53. if ((self = [super initWithCoder:aDecoder])) {
  54. [self commonInitialiser];
  55. }
  56. return self;
  57. }
  58. - (id)initWithFrame:(CGRect)frame {
  59. if ((self = [super initWithFrame:frame])) {
  60. [self commonInitialiser];
  61. }
  62. return self;
  63. }
  64. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  65. - (id)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer {
  66. if ((self = [super initWithFrame:frame])) {
  67. [self commonInitialiser:textContainer];
  68. }
  69. return self;
  70. }
  71. -(void)commonInitialiser {
  72. [self commonInitialiser:nil];
  73. }
  74. -(void)commonInitialiser:(NSTextContainer *)textContainer
  75. #else
  76. -(void)commonInitialiser
  77. #endif
  78. {
  79. // Initialization code
  80. CGRect r = self.frame;
  81. r.origin.y = 0;
  82. r.origin.x = 0;
  83. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  84. internalTextView = [[HPTextViewInternal alloc] initWithFrame:r textContainer:textContainer];
  85. #else
  86. internalTextView = [[HPTextViewInternal alloc] initWithFrame:r];
  87. #endif
  88. internalTextView.delegate = self;
  89. internalTextView.scrollEnabled = NO;
  90. internalTextView.font = [UIFont fontWithName:@"Helvetica" size:13];
  91. internalTextView.contentInset = UIEdgeInsetsZero;
  92. internalTextView.showsHorizontalScrollIndicator = NO;
  93. internalTextView.text = @"-";
  94. internalTextView.contentMode = UIViewContentModeRedraw;
  95. if (@available(iOS 11.0, *)) {
  96. internalTextView.smartDashesType = UITextSmartDashesTypeNo;
  97. internalTextView.smartQuotesType = UITextSmartDashesTypeNo;
  98. }
  99. [self addSubview:internalTextView];
  100. minHeight = internalTextView.frame.size.height;
  101. minNumberOfLines = 1;
  102. animateHeightChange = YES;
  103. animationDuration = 0.1f;
  104. internalTextView.text = @"";
  105. [self setMaxNumberOfLines:3];
  106. [self setPlaceholderColor:[UIColor lightGrayColor]];
  107. internalTextView.displayPlaceHolder = YES;
  108. /* make a dummy text view with the same font for use in height calculations */
  109. dummyTextView = [[UITextView alloc] init];
  110. dummyTextView.scrollEnabled = NO;
  111. dummyTextView.font = internalTextView.font;
  112. dummyTextView.contentInset = UIEdgeInsetsZero;
  113. dummyTextView.showsHorizontalScrollIndicator = NO;
  114. if (@available(iOS 11.0, *)) {
  115. dummyTextView.smartDashesType = UITextSmartDashesTypeNo;
  116. dummyTextView.smartQuotesType = UITextSmartDashesTypeNo;
  117. }
  118. }
  119. -(CGSize)sizeThatFits:(CGSize)size
  120. {
  121. if (self.text.length == 0) {
  122. size.height = minHeight;
  123. }
  124. return size;
  125. }
  126. -(void)layoutSubviews
  127. {
  128. [super layoutSubviews];
  129. CGRect r = self.bounds;
  130. r.origin.y = 0;
  131. r.origin.x = contentInset.left;
  132. r.size.width -= contentInset.left + contentInset.right;
  133. internalTextView.frame = r;
  134. }
  135. -(void)setContentInset:(UIEdgeInsets)inset
  136. {
  137. contentInset = inset;
  138. CGRect r = self.frame;
  139. r.origin.y = inset.top - inset.bottom;
  140. r.origin.x = inset.left;
  141. r.size.width -= inset.left + inset.right;
  142. internalTextView.frame = r;
  143. [self setMaxNumberOfLines:maxNumberOfLines];
  144. [self setMinNumberOfLines:minNumberOfLines];
  145. }
  146. -(UIEdgeInsets)contentInset
  147. {
  148. return contentInset;
  149. }
  150. -(void)setMaxNumberOfLines:(int)n
  151. {
  152. if(n == 0 && maxHeight > 0) return; // the user specified a maxHeight themselves.
  153. // Use internalTextView for height calculations, thanks to Gwynne <http://blog.darkrainfall.org/>
  154. NSString *saveText = internalTextView.text, *newText = @"-";
  155. internalTextView.delegate = nil;
  156. internalTextView.hidden = YES;
  157. for (int i = 1; i < n; ++i)
  158. newText = [newText stringByAppendingString:@"\n|W|"];
  159. internalTextView.text = newText;
  160. maxHeight = ceilf([self measureHeight]);
  161. internalTextView.text = saveText;
  162. internalTextView.hidden = NO;
  163. internalTextView.delegate = self;
  164. [self sizeToFit];
  165. maxNumberOfLines = n;
  166. }
  167. -(int)maxNumberOfLines
  168. {
  169. return maxNumberOfLines;
  170. }
  171. - (void)setMaxHeight:(int)height
  172. {
  173. maxHeight = height;
  174. maxNumberOfLines = 0;
  175. }
  176. -(void)setMinNumberOfLines:(int)m
  177. {
  178. if(m == 0 && minHeight > 0) return; // the user specified a minHeight themselves.
  179. // Use internalTextView for height calculations, thanks to Gwynne <http://blog.darkrainfall.org/>
  180. NSString *saveText = internalTextView.text, *newText = @"-";
  181. internalTextView.delegate = nil;
  182. internalTextView.hidden = YES;
  183. for (int i = 1; i < m; ++i)
  184. newText = [newText stringByAppendingString:@"\n|W|"];
  185. internalTextView.text = newText;
  186. minHeight = ceilf([self measureHeight]);
  187. internalTextView.text = saveText;
  188. internalTextView.hidden = NO;
  189. internalTextView.delegate = self;
  190. [self sizeToFit];
  191. minNumberOfLines = m;
  192. }
  193. -(int)minNumberOfLines
  194. {
  195. return minNumberOfLines;
  196. }
  197. - (void)setMinHeight:(int)height
  198. {
  199. minHeight = height;
  200. minNumberOfLines = 0;
  201. }
  202. - (NSString *)placeholder
  203. {
  204. return internalTextView.placeholder;
  205. }
  206. - (void)setPlaceholder:(NSString *)placeholder
  207. {
  208. [internalTextView setPlaceholder:placeholder];
  209. [internalTextView setNeedsDisplay];
  210. }
  211. - (UIColor *)placeholderColor
  212. {
  213. return internalTextView.placeholderColor;
  214. }
  215. - (void)setPlaceholderColor:(UIColor *)placeholderColor
  216. {
  217. [internalTextView setPlaceholderColor:placeholderColor];
  218. }
  219. - (void)textViewDidChange:(UITextView *)textView
  220. {
  221. [self refreshHeight];
  222. }
  223. - (void)refreshHeight {
  224. [self refreshHeightForce:NO];
  225. }
  226. - (void)refreshHeightForce:(BOOL)force
  227. {
  228. //size of content, so we can set the frame of self
  229. NSInteger newSizeH = ceilf([self measureHeight]);
  230. if (newSizeH < minHeight || !internalTextView.hasText) {
  231. newSizeH = minHeight; //not smalles than minHeight
  232. }
  233. else if (maxHeight && newSizeH > maxHeight) {
  234. newSizeH = maxHeight; // not taller than maxHeight
  235. }
  236. if (force || internalTextView.frame.size.height != newSizeH)
  237. {
  238. // if our new height is greater than the maxHeight
  239. // sets not set the height or move things
  240. // around and enable scrolling
  241. if (newSizeH >= maxHeight)
  242. {
  243. if(!internalTextView.scrollEnabled){
  244. internalTextView.scrollEnabled = YES;
  245. [internalTextView flashScrollIndicators];
  246. }
  247. } else {
  248. internalTextView.scrollEnabled = NO;
  249. }
  250. // [fixed] Pasting too much text into the view failed to fire the height change,
  251. // thanks to Gwynne <http://blog.darkrainfall.org/>
  252. if (newSizeH <= maxHeight)
  253. {
  254. if(animateHeightChange) {
  255. if ([UIView resolveClassMethod:@selector(animateWithDuration:animations:)]) {
  256. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
  257. [UIView animateWithDuration:animationDuration
  258. delay:0
  259. options:(UIViewAnimationOptionAllowUserInteraction|
  260. UIViewAnimationOptionBeginFromCurrentState)
  261. animations:^(void) {
  262. [self resizeTextView:newSizeH];
  263. }
  264. completion:^(BOOL finished) {
  265. if ([delegate respondsToSelector:@selector(growingTextView:didChangeHeight:)]) {
  266. [delegate growingTextView:self didChangeHeight:newSizeH];
  267. }
  268. }];
  269. #endif
  270. } else {
  271. [UIView beginAnimations:@"" context:nil];
  272. [UIView setAnimationDuration:animationDuration];
  273. [UIView setAnimationDelegate:self];
  274. [UIView setAnimationDidStopSelector:@selector(growDidStop)];
  275. [UIView setAnimationBeginsFromCurrentState:YES];
  276. [self resizeTextView:newSizeH];
  277. [UIView commitAnimations];
  278. }
  279. } else {
  280. [self resizeTextView:newSizeH];
  281. // [fixed] The growingTextView:didChangeHeight: delegate method was not called at all when not animating height changes.
  282. // thanks to Gwynne <http://blog.darkrainfall.org/>
  283. if ([delegate respondsToSelector:@selector(growingTextView:didChangeHeight:)]) {
  284. [delegate growingTextView:self didChangeHeight:newSizeH];
  285. }
  286. }
  287. }
  288. }
  289. // Display (or not) the placeholder string
  290. BOOL wasDisplayingPlaceholder = internalTextView.displayPlaceHolder;
  291. internalTextView.displayPlaceHolder = self.internalTextView.text.length == 0;
  292. if (wasDisplayingPlaceholder != internalTextView.displayPlaceHolder) {
  293. [internalTextView setNeedsDisplay];
  294. }
  295. // Tell the delegate that the text view changed
  296. if ([delegate respondsToSelector:@selector(growingTextViewDidChange:)]) {
  297. [delegate growingTextViewDidChange:self];
  298. }
  299. }
  300. - (CGFloat)measureHeight
  301. {
  302. if ([self respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)])
  303. {
  304. if (internalTextView.attributedText && internalTextView.attributedText.length > 0)
  305. dummyTextView.attributedText = internalTextView.attributedText;
  306. else
  307. dummyTextView.text = internalTextView.text;
  308. CGSize size = [dummyTextView sizeThatFits:CGSizeMake(internalTextView.bounds.size.width, CGFLOAT_MAX)];
  309. return size.height;
  310. }
  311. else
  312. {
  313. return self.internalTextView.contentSize.height;
  314. }
  315. }
  316. -(void)resizeTextView:(NSInteger)newSizeH
  317. {
  318. if ([delegate respondsToSelector:@selector(growingTextView:willChangeHeight:)]) {
  319. [delegate growingTextView:self willChangeHeight:newSizeH];
  320. }
  321. CGRect internalTextViewFrame = self.frame;
  322. internalTextViewFrame.size.height = newSizeH; // + padding
  323. self.frame = internalTextViewFrame;
  324. internalTextViewFrame.origin.y = contentInset.top - contentInset.bottom;
  325. internalTextViewFrame.origin.x = contentInset.left;
  326. if(!CGRectEqualToRect(internalTextView.frame, internalTextViewFrame)) internalTextView.frame = internalTextViewFrame;
  327. }
  328. - (void)growDidStop
  329. {
  330. if ([delegate respondsToSelector:@selector(growingTextView:didChangeHeight:)]) {
  331. [delegate growingTextView:self didChangeHeight:self.frame.size.height];
  332. }
  333. }
  334. -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  335. {
  336. [internalTextView becomeFirstResponder];
  337. }
  338. - (BOOL)becomeFirstResponder
  339. {
  340. [super becomeFirstResponder];
  341. return [self.internalTextView becomeFirstResponder];
  342. }
  343. -(BOOL)resignFirstResponder
  344. {
  345. [super resignFirstResponder];
  346. return [internalTextView resignFirstResponder];
  347. }
  348. -(BOOL)isFirstResponder
  349. {
  350. return [self.internalTextView isFirstResponder];
  351. }
  352. ///////////////////////////////////////////////////////////////////////////////////////////////////
  353. #pragma mark UITextView properties
  354. ///////////////////////////////////////////////////////////////////////////////////////////////////
  355. -(void)setText:(NSString *)newText
  356. {
  357. internalTextView.text = newText;
  358. // include this line to analyze the height of the textview.
  359. // fix from Ankit Thakur
  360. [self performSelector:@selector(textViewDidChange:) withObject:internalTextView];
  361. }
  362. -(NSString*) text
  363. {
  364. return internalTextView.text;
  365. }
  366. -(void)setAttributedText:(NSAttributedString *)attributedText
  367. {
  368. internalTextView.attributedText = attributedText;
  369. internalTextView.typingAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor], NSFontAttributeName:[UIFont systemFontOfSize:16]};
  370. // include this line to analyze the height of the textview.
  371. // fix from Ankit Thakur
  372. [self performSelector:@selector(textViewDidChange:) withObject:internalTextView];
  373. }
  374. -(NSAttributedString*)attributedText
  375. {
  376. return internalTextView.attributedText;
  377. }
  378. ///////////////////////////////////////////////////////////////////////////////////////////////////
  379. -(void)setFont:(UIFont *)afont
  380. {
  381. internalTextView.font = afont;
  382. dummyTextView.font = afont;
  383. [self setMaxNumberOfLines:maxNumberOfLines];
  384. [self setMinNumberOfLines:minNumberOfLines];
  385. }
  386. -(UIFont *)font
  387. {
  388. return internalTextView.font;
  389. }
  390. ///////////////////////////////////////////////////////////////////////////////////////////////////
  391. -(void)setTextColor:(UIColor *)color
  392. {
  393. internalTextView.textColor = color;
  394. }
  395. -(UIColor*)textColor{
  396. return internalTextView.textColor;
  397. }
  398. ///////////////////////////////////////////////////////////////////////////////////////////////////
  399. -(void)setBackgroundColor:(UIColor *)backgroundColor
  400. {
  401. [super setBackgroundColor:backgroundColor];
  402. internalTextView.backgroundColor = backgroundColor;
  403. }
  404. -(UIColor*)backgroundColor
  405. {
  406. return internalTextView.backgroundColor;
  407. }
  408. ///////////////////////////////////////////////////////////////////////////////////////////////////
  409. -(void)setTextAlignment:(NSTextAlignment)aligment
  410. {
  411. internalTextView.textAlignment = aligment;
  412. }
  413. -(NSTextAlignment)textAlignment
  414. {
  415. return internalTextView.textAlignment;
  416. }
  417. ///////////////////////////////////////////////////////////////////////////////////////////////////
  418. -(void)setSelectedRange:(NSRange)range
  419. {
  420. internalTextView.selectedRange = range;
  421. }
  422. -(NSRange)selectedRange
  423. {
  424. return internalTextView.selectedRange;
  425. }
  426. ///////////////////////////////////////////////////////////////////////////////////////////////////
  427. - (void)setIsScrollable:(BOOL)isScrollable
  428. {
  429. internalTextView.scrollEnabled = isScrollable;
  430. }
  431. - (BOOL)isScrollable
  432. {
  433. return internalTextView.scrollEnabled;
  434. }
  435. ///////////////////////////////////////////////////////////////////////////////////////////////////
  436. -(void)setEditable:(BOOL)beditable
  437. {
  438. internalTextView.editable = beditable;
  439. }
  440. -(BOOL)isEditable
  441. {
  442. return internalTextView.editable;
  443. }
  444. ///////////////////////////////////////////////////////////////////////////////////////////////////
  445. -(void)setReturnKeyType:(UIReturnKeyType)keyType
  446. {
  447. internalTextView.returnKeyType = keyType;
  448. }
  449. -(UIReturnKeyType)returnKeyType
  450. {
  451. return internalTextView.returnKeyType;
  452. }
  453. ///////////////////////////////////////////////////////////////////////////////////////////////////
  454. - (void)setKeyboardType:(UIKeyboardType)keyType
  455. {
  456. internalTextView.keyboardType = keyType;
  457. }
  458. - (UIKeyboardType)keyboardType
  459. {
  460. return internalTextView.keyboardType;
  461. }
  462. ///////////////////////////////////////////////////////////////////////////////////////////////////
  463. - (void)setKeyboardAppearance:(UIKeyboardAppearance)keyboardAppearance
  464. {
  465. internalTextView.keyboardAppearance = keyboardAppearance;
  466. }
  467. - (UIKeyboardAppearance)keyboardAppearance
  468. {
  469. return internalTextView.keyboardAppearance;
  470. }
  471. ///////////////////////////////////////////////////////////////////////////////////////////////////
  472. - (void)setEnablesReturnKeyAutomatically:(BOOL)enablesReturnKeyAutomatically
  473. {
  474. internalTextView.enablesReturnKeyAutomatically = enablesReturnKeyAutomatically;
  475. }
  476. - (BOOL)enablesReturnKeyAutomatically
  477. {
  478. return internalTextView.enablesReturnKeyAutomatically;
  479. }
  480. ///////////////////////////////////////////////////////////////////////////////////////////////////
  481. -(void)setDataDetectorTypes:(UIDataDetectorTypes)datadetector
  482. {
  483. internalTextView.dataDetectorTypes = datadetector;
  484. }
  485. -(UIDataDetectorTypes)dataDetectorTypes
  486. {
  487. return internalTextView.dataDetectorTypes;
  488. }
  489. ///////////////////////////////////////////////////////////////////////////////////////////////////
  490. - (BOOL)hasText{
  491. return [internalTextView hasText];
  492. }
  493. - (void)scrollRangeToVisible:(NSRange)range
  494. {
  495. [internalTextView scrollRangeToVisible:range];
  496. }
  497. /////////////////////////////////////////////////////////////////////////////////////////////////////
  498. /////////////////////////////////////////////////////////////////////////////////////////////////////
  499. #pragma mark -
  500. #pragma mark UITextViewDelegate
  501. ///////////////////////////////////////////////////////////////////////////////////////////////////
  502. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
  503. if ([delegate respondsToSelector:@selector(growingTextViewShouldBeginEditing:)]) {
  504. return [delegate growingTextViewShouldBeginEditing:self];
  505. } else {
  506. return YES;
  507. }
  508. }
  509. ///////////////////////////////////////////////////////////////////////////////////////////////////
  510. - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
  511. if ([delegate respondsToSelector:@selector(growingTextViewShouldEndEditing:)]) {
  512. return [delegate growingTextViewShouldEndEditing:self];
  513. } else {
  514. return YES;
  515. }
  516. }
  517. ///////////////////////////////////////////////////////////////////////////////////////////////////
  518. - (void)textViewDidBeginEditing:(UITextView *)textView {
  519. if ([delegate respondsToSelector:@selector(growingTextViewDidBeginEditing:)]) {
  520. [delegate growingTextViewDidBeginEditing:self];
  521. }
  522. }
  523. ///////////////////////////////////////////////////////////////////////////////////////////////////
  524. - (void)textViewDidEndEditing:(UITextView *)textView {
  525. if ([delegate respondsToSelector:@selector(growingTextViewDidEndEditing:)]) {
  526. [delegate growingTextViewDidEndEditing:self];
  527. }
  528. }
  529. ///////////////////////////////////////////////////////////////////////////////////////////////////
  530. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
  531. replacementText:(NSString *)atext {
  532. //weird 1 pixel bug when clicking backspace when textView is empty
  533. if(![textView hasText] && [atext isEqualToString:@""]) return NO;
  534. //Added by bretdabaker: sometimes we want to handle this ourselves
  535. if ([delegate respondsToSelector:@selector(growingTextView:shouldChangeTextInRange:replacementText:)])
  536. return [delegate growingTextView:self shouldChangeTextInRange:range replacementText:atext];
  537. if ([atext isEqualToString:@"\n"]) {
  538. if ([delegate respondsToSelector:@selector(growingTextViewShouldReturn:)]) {
  539. if (![delegate performSelector:@selector(growingTextViewShouldReturn:) withObject:self]) {
  540. return YES;
  541. } else {
  542. [textView resignFirstResponder];
  543. return NO;
  544. }
  545. }
  546. }
  547. return YES;
  548. }
  549. ///////////////////////////////////////////////////////////////////////////////////////////////////
  550. - (void)textViewDidChangeSelection:(UITextView *)textView {
  551. if ([delegate respondsToSelector:@selector(growingTextViewDidChangeSelection:)]) {
  552. [delegate growingTextViewDidChangeSelection:self];
  553. }
  554. }
  555. @end