HPGrowingTextView.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // HPTextView.h
  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 <UIKit/UIKit.h>
  28. #if __IPHONE_OS_VERSION_MAX_ALLOWED < 60000
  29. // UITextAlignment is deprecated in iOS 6.0+, use NSTextAlignment instead.
  30. // Reference: https://developer.apple.com/library/ios/documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html
  31. #define NSTextAlignment UITextAlignment
  32. #endif
  33. @class HPGrowingTextView;
  34. @class HPTextViewInternal;
  35. @protocol HPGrowingTextViewDelegate
  36. @optional
  37. - (BOOL)growingTextViewShouldBeginEditing:(HPGrowingTextView *)growingTextView;
  38. - (BOOL)growingTextViewShouldEndEditing:(HPGrowingTextView *)growingTextView;
  39. - (void)growingTextViewDidBeginEditing:(HPGrowingTextView *)growingTextView;
  40. - (void)growingTextViewDidEndEditing:(HPGrowingTextView *)growingTextView;
  41. - (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
  42. - (void)growingTextViewDidChange:(HPGrowingTextView *)growingTextView;
  43. - (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height;
  44. - (void)growingTextView:(HPGrowingTextView *)growingTextView didChangeHeight:(float)height;
  45. - (void)growingTextViewDidChangeSelection:(HPGrowingTextView *)growingTextView;
  46. - (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView;
  47. @end
  48. @interface HPGrowingTextView : UIView <UITextViewDelegate> {
  49. HPTextViewInternal *internalTextView;
  50. int minHeight;
  51. int maxHeight;
  52. //class properties
  53. int maxNumberOfLines;
  54. int minNumberOfLines;
  55. BOOL animateHeightChange;
  56. NSTimeInterval animationDuration;
  57. //uitextview properties
  58. NSObject <HPGrowingTextViewDelegate> *__unsafe_unretained delegate;
  59. NSTextAlignment textAlignment;
  60. NSRange selectedRange;
  61. BOOL editable;
  62. UIDataDetectorTypes dataDetectorTypes;
  63. UIReturnKeyType returnKeyType;
  64. UIKeyboardType keyboardType;
  65. UIEdgeInsets contentInset;
  66. UITextView *dummyTextView;
  67. }
  68. //real class properties
  69. @property int maxNumberOfLines;
  70. @property int minNumberOfLines;
  71. @property (nonatomic) int maxHeight;
  72. @property (nonatomic) int minHeight;
  73. @property BOOL animateHeightChange;
  74. @property NSTimeInterval animationDuration;
  75. @property (nonatomic, strong) NSString *placeholder;
  76. @property (nonatomic, strong) UIColor *placeholderColor;
  77. @property (nonatomic, strong) UITextView *internalTextView;
  78. //uitextview properties
  79. @property(unsafe_unretained) NSObject<HPGrowingTextViewDelegate> *delegate;
  80. @property(nonatomic,strong) NSString *text;
  81. @property(nonatomic,strong) NSAttributedString *attributedText;
  82. @property(nonatomic,strong) UIFont *font;
  83. @property(nonatomic,strong) UIColor *textColor;
  84. @property(nonatomic) NSTextAlignment textAlignment; // default is NSTextAlignmentLeft
  85. @property(nonatomic) NSRange selectedRange; // only ranges of length 0 are supported
  86. @property(nonatomic,getter=isEditable) BOOL editable;
  87. @property(nonatomic) UIDataDetectorTypes dataDetectorTypes __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_3_0);
  88. @property (nonatomic) UIReturnKeyType returnKeyType;
  89. @property (nonatomic) UIKeyboardType keyboardType;
  90. @property (nonatomic) UIKeyboardAppearance keyboardAppearance;
  91. @property (assign) UIEdgeInsets contentInset;
  92. @property (nonatomic) BOOL isScrollable;
  93. @property(nonatomic) BOOL enablesReturnKeyAutomatically;
  94. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  95. - (id)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer;
  96. #endif
  97. //uitextview methods
  98. //need others? use .internalTextView
  99. - (BOOL)becomeFirstResponder;
  100. - (BOOL)resignFirstResponder;
  101. - (BOOL)isFirstResponder;
  102. - (BOOL)hasText;
  103. - (void)scrollRangeToVisible:(NSRange)range;
  104. // call to force a height change (e.g. after you change max/min lines)
  105. - (void)refreshHeight;
  106. - (void)refreshHeightForce:(BOOL)force;
  107. @end