|
| 1 | +// |
| 2 | +// CSGrowingTextView.h |
| 3 | +// CSGrowingTextView |
| 4 | +// |
| 5 | +// Created by Josip Bernat on 01/03/14. |
| 6 | +// Copyright (c) 2014 Clover-Studio. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import <UIKit/UIKit.h> |
| 10 | + |
| 11 | +typedef NS_ENUM(NSInteger, CSGrowDirection) { |
| 12 | + CSGrowDirectionUp = 0, |
| 13 | + CSGrowDirectionDown, |
| 14 | + CSGrowDirectionNone ///CSGrowingTextView will not grow. Usefull for constraints where UIViewContoller updates height of CSGrowingTextView manually. |
| 15 | +}; |
| 16 | + |
| 17 | +@protocol CSGrowingTextViewDelegate; |
| 18 | + |
| 19 | +/** |
| 20 | + * The CSGrowingTextView class implements the behavior for a scrollable, multiline text region that grows / shrinks while adding user inputs new text. Using minimumNumberOfLines and maximumNumberOfLines you limit number of lines displayed without shrinking / growing. |
| 21 | + */ |
| 22 | + |
| 23 | +@interface CSGrowingTextView : UIView <UITextViewDelegate> |
| 24 | + |
| 25 | +/** |
| 26 | + * The receiver’s delegate. |
| 27 | + */ |
| 28 | +@property (nonatomic, weak) id<CSGrowingTextViewDelegate> delegate; |
| 29 | + |
| 30 | +/** |
| 31 | + * Text view used for the main textual content of the growint text view. You should be careful with direct changing it's attributes since it may affect growing text view shrinking and growing process. |
| 32 | + */ |
| 33 | +@property (nonatomic, strong, readonly) UITextView *textView; |
| 34 | + |
| 35 | +/** |
| 36 | + * Label used for the placeholder textual content of the growing text view. |
| 37 | + */ |
| 38 | +@property (nonatomic, strong, readonly) UILabel *placeholderLabel; |
| 39 | + |
| 40 | +/** |
| 41 | + * Minimum number of lines displayed inside growing text view without any more shrinking. Default is 1. |
| 42 | + */ |
| 43 | +@property (nonatomic, readwrite) NSUInteger minimumNumberOfLines; |
| 44 | + |
| 45 | +/** |
| 46 | + * Maximum number of lines displayed inside growing text view without any more resizing. Default is 3. |
| 47 | + */ |
| 48 | +@property (nonatomic, readwrite) NSUInteger maximumNumberOfLines; |
| 49 | + |
| 50 | +/** |
| 51 | + * Boolean value determening if newline character should be enabled, e.g if newline character should be treated as return key or not. Default value is NO. |
| 52 | + */ |
| 53 | +@property (nonatomic, readwrite) BOOL enablesNewlineCharacter; |
| 54 | + |
| 55 | +/** |
| 56 | + * CSGrowDirection value determening in what direction should growing text view grow or shring. Default value is CSGrowDirectionUp. |
| 57 | + */ |
| 58 | +@property (nonatomic, readwrite) CSGrowDirection growDirection; |
| 59 | + |
| 60 | +@end |
| 61 | + |
| 62 | +@protocol CSGrowingTextViewDelegate <NSObject> |
| 63 | + |
| 64 | +@optional; |
| 65 | + |
| 66 | +#pragma mark - Responding to Editing Notifications |
| 67 | + |
| 68 | +/** |
| 69 | + * Asks the delegate if editing should begin in the specified growing text view. Implementation of this method is optional, if it is not present, editing proceeds as if this method had returned YES. This method forwards UITextViewDelegate textViewShouldBeginEditing: method call. |
| 70 | + * |
| 71 | + * @param textView The growing text view for which editing is about to begin. |
| 72 | + * |
| 73 | + * @return YES if an editing session should be initiated; otherwise, NO to disallow editing. |
| 74 | + */ |
| 75 | +- (BOOL)growingTextViewShouldBeginEditing:(CSGrowingTextView *)textView; |
| 76 | + |
| 77 | +/** |
| 78 | + * Tells the delegate that editing of the specified growing text view has begun. Implementation of this method is optional. This method forwards UITextViewDelegate textViewDidBeginEditing: method call. |
| 79 | + * |
| 80 | + * @param textView The growing text view in which editing began. |
| 81 | + */ |
| 82 | +- (void)growingTextViewDidBeginEditing:(CSGrowingTextView *)textView; |
| 83 | + |
| 84 | +/** |
| 85 | + * Asks the delegate if editing should stop in the specified growing text view. Implementation of this method is optional, if it is not present, editing proceeds as if this method had returned YES. This method forwards UITextViewDelegate textViewShouldEndEditing: method call. |
| 86 | + * |
| 87 | + * @param textView The text view for which editing is about to end. |
| 88 | + * |
| 89 | + * @return YES if editing should stop; otherwise, NO if the editing session should continue. |
| 90 | + */ |
| 91 | +- (BOOL)growingTextViewShouldEndEditing:(CSGrowingTextView *)textView; |
| 92 | + |
| 93 | +/** |
| 94 | + * Tells the delegate that editing of the specified growing text view has ended. Implementation of this method is optional, if it is not present, editing proceeds as if this method had returned YES. This method forwards UITextViewDelegate textViewDidEndEditing: method call. |
| 95 | + * |
| 96 | + * @param textView The growing text view in which editing ended. |
| 97 | + */ |
| 98 | +- (void)growingTextViewDidEndEditing:(CSGrowingTextView *)textView; |
| 99 | + |
| 100 | +/** |
| 101 | + * Asks the delegate if the growing text field should process the pressing of the return button. Method is called only when enablesNewlineCharacter is set to NO. Implementation of this method is optional, if it is not present, editing proceeds as if this method had returned NO. |
| 102 | + * |
| 103 | + * @param textView The growing text view whose return button was pressed. |
| 104 | + * |
| 105 | + * @return YES if the growing text view should implement its default behavior for the return button; otherwise, NO. |
| 106 | + */ |
| 107 | +- (BOOL)growingTextViewShouldReturn:(CSGrowingTextView *)textView; |
| 108 | + |
| 109 | +#pragma mark - Responding to Text Changes |
| 110 | + |
| 111 | +/** |
| 112 | + * Asks the delegate whether the specified text should be replaced in the text view. Implementation of this method is optional, if it is not present, editing proceeds as if this method had returned YES. This method forwards UITextViewDelegate textView:shouldChangeTextInRange:replacementText: method call. |
| 113 | + * |
| 114 | + * @param textView The text view containing the changes. |
| 115 | + * @param range The current selection range. If the length of the range is 0, range reflects the current insertion point. If the user presses the Delete key, the length of the range is 1 and an empty string object replaces that single character. |
| 116 | + * @param text The text to insert. |
| 117 | + * |
| 118 | + * @return YES if the old text should be replaced by the new text; NO if the replacement operation should be aborted. |
| 119 | + */ |
| 120 | +- (BOOL)growingTextView:(CSGrowingTextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text; |
| 121 | + |
| 122 | +/** |
| 123 | + * Tells the delegate that the text or attributes in the specified growing text view were changed by the user. Implementation of this method is optional, if it is not present, editing proceeds as if this method had returned YES. This method forwards UITextViewDelegate textViewDidChange: method call. |
| 124 | + * |
| 125 | + * @param textView The growing text view containing the changes. |
| 126 | + */ |
| 127 | +- (void)growingTextViewDidChange:(CSGrowingTextView *)textView; |
| 128 | + |
| 129 | +#pragma mark - Responding to Selection Changes |
| 130 | + |
| 131 | +/** |
| 132 | + * Tells the delegate that the text selection changed in the specified growing text view. Implementation of this method is optional, if it is not present, editing proceeds as if this method had returned YES. This method forwards UITextViewDelegate textViewDidChangeSelection: method call. |
| 133 | + * |
| 134 | + * @param textView The growing text view whose selection changed. |
| 135 | + */ |
| 136 | +- (void)growingTextViewDidChangeSelection:(CSGrowingTextView *)textView; |
| 137 | + |
| 138 | +#pragma mark - Interacting with Text Data |
| 139 | + |
| 140 | +/** |
| 141 | + * Asks the delegate if the specified growing text view should allow user interaction with the provided text attachment in the given range of text. Implementation of this method is optional, if it is not present, editing proceeds as if this method had returned YES. This method forwards UITextViewDelegate textView:shouldInteractWithTextAttachment:inRange: method call. |
| 142 | + * |
| 143 | + * @param textView The growing text view containing the text attachment. |
| 144 | + * @param textAttachment The text attachment. |
| 145 | + * @param characterRange The character range containing the text attachment. |
| 146 | + * |
| 147 | + * @return YES if interaction with the text attachment should be allowed; NO if interaction should not be allowed. |
| 148 | + */ |
| 149 | +- (BOOL)growingTextView:(CSGrowingTextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0); |
| 150 | + |
| 151 | +/** |
| 152 | + * Asks the delegate if the specified growing text view should allow user interaction with the given URL in the given range of text. Implementation of this method is optional, if it is not present, editing proceeds as if this method had returned YES. This method forwards UITextViewDelegate textView:shouldInteractWithURL:inRange: method call. |
| 153 | + * |
| 154 | + * @param textView The growing text view containing the text attachment. |
| 155 | + * @param URL The URL to be processed. |
| 156 | + * @param characterRange The character range containing the URL. |
| 157 | + * |
| 158 | + * @return YES if interaction with the URL should be allowed; NO if interaction should not be allowed. |
| 159 | + */ |
| 160 | +- (BOOL)growingTextView:(CSGrowingTextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0); |
| 161 | + |
| 162 | +#pragma mark - Height Notifications |
| 163 | + |
| 164 | +/** |
| 165 | + * Tells the delegate growing text view is about to change height. |
| 166 | + * |
| 167 | + * @param growingTextView The growing text view whose height will change. |
| 168 | + * @param height The future height of the growing text view. |
| 169 | + */ |
| 170 | +- (void)growingTextView:(CSGrowingTextView *)growingTextView willChangeHeight:(CGFloat)height; |
| 171 | + |
| 172 | +/** |
| 173 | + * Tells the delegate growing text view did change height. |
| 174 | + * |
| 175 | + * @param growingTextView The growing text view whose height did change. |
| 176 | + * @param height The new height of the growing text view. |
| 177 | + */ |
| 178 | +- (void)growingTextView:(CSGrowingTextView *)growingTextView didChangeHeight:(CGFloat)height; |
| 179 | + |
| 180 | +@end |
0 commit comments