HPTextViewInternal.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // HPTextViewInternal.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 "HPTextViewInternal.h"
  28. @implementation HPTextViewInternal
  29. -(void)setText:(NSString *)text
  30. {
  31. BOOL originalValue = self.scrollEnabled;
  32. //If one of GrowingTextView's superviews is a scrollView, and self.scrollEnabled == NO,
  33. //setting the text programatically will cause UIKit to search upwards until it finds a scrollView with scrollEnabled==yes
  34. //then scroll it erratically. Setting scrollEnabled temporarily to YES prevents this.
  35. [self setScrollEnabled:YES];
  36. [super setText:text];
  37. [self setScrollEnabled:originalValue];
  38. }
  39. - (void)setScrollable:(BOOL)isScrollable
  40. {
  41. [super setScrollEnabled:isScrollable];
  42. }
  43. -(void)setContentOffset:(CGPoint)s
  44. {
  45. if(self.tracking || self.decelerating){
  46. //initiated by user...
  47. UIEdgeInsets insets = self.contentInset;
  48. insets.bottom = 0;
  49. insets.top = 0;
  50. self.contentInset = insets;
  51. } else {
  52. float bottomOffset = (self.contentSize.height - self.frame.size.height + self.contentInset.bottom);
  53. if(s.y < bottomOffset && self.scrollEnabled){
  54. UIEdgeInsets insets = self.contentInset;
  55. insets.bottom = 8;
  56. insets.top = 0;
  57. self.contentInset = insets;
  58. }
  59. }
  60. // Fix "overscrolling" bug
  61. if (s.y > self.contentSize.height - self.frame.size.height && !self.decelerating && !self.tracking && !self.dragging)
  62. s = CGPointMake(s.x, self.contentSize.height - self.frame.size.height);
  63. [super setContentOffset:s];
  64. }
  65. -(void)setContentInset:(UIEdgeInsets)s
  66. {
  67. UIEdgeInsets insets = s;
  68. if(s.bottom>8) insets.bottom = 0;
  69. insets.top = 0;
  70. [super setContentInset:insets];
  71. }
  72. -(void)setContentSize:(CGSize)contentSize
  73. {
  74. // is this an iOS5 bug? Need testing!
  75. if(self.contentSize.height > contentSize.height)
  76. {
  77. UIEdgeInsets insets = self.contentInset;
  78. insets.bottom = 0;
  79. insets.top = 0;
  80. self.contentInset = insets;
  81. }
  82. [super setContentSize:contentSize];
  83. }
  84. - (void)drawRect:(CGRect)rect
  85. {
  86. [super drawRect:rect];
  87. if (self.displayPlaceHolder && self.placeholder && self.placeholderColor)
  88. {
  89. if ([self respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)])
  90. {
  91. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  92. paragraphStyle.alignment = self.textAlignment;
  93. [self.placeholder drawInRect:CGRectMake(5, 8 + self.contentInset.top, self.frame.size.width-self.contentInset.left, self.frame.size.height- self.contentInset.top) withAttributes:@{NSFontAttributeName:self.font, NSForegroundColorAttributeName:self.placeholderColor, NSParagraphStyleAttributeName:paragraphStyle}];
  94. }
  95. else {
  96. [self.placeholderColor set];
  97. [self.placeholder drawInRect:CGRectMake(8.0f, 8.0f, self.frame.size.width - 16.0f, self.frame.size.height - 16.0f) withAttributes:@{NSFontAttributeName : self.font}];
  98. }
  99. }
  100. }
  101. -(void)setPlaceholder:(NSString *)placeholder
  102. {
  103. _placeholder = placeholder;
  104. [self setNeedsDisplay];
  105. }
  106. @end