diff --git a/RDRStickyKeyboardView/RDRStickyKeyboardView.h b/RDRStickyKeyboardView/RDRStickyKeyboardView.h index 6e92dd3..c949d6e 100644 --- a/RDRStickyKeyboardView/RDRStickyKeyboardView.h +++ b/RDRStickyKeyboardView/RDRStickyKeyboardView.h @@ -52,10 +52,29 @@ @end +#pragma mark - RDRStickyKeyboardViewDelegate + +@protocol RDRStickyKeyboardViewDelegate + +-(void)didSelectLeftKeyboardButton; +-(void)didSelectRightKeyboardButton; + +@end + #pragma mark - RDRStickyKeyboardView @interface RDRStickyKeyboardView : UIView +/** + * Delegate to notify about left/right button actions. + */ +@property(nonatomic, weak) id delegate; + +/** + * The UITextView instance from the inputViewScrollView to be accessed by external classes to be able to handle text content and delegation. + */ +@property(nonatomic, readonly) UITextView * textView; + @property (nonatomic, strong, readonly) UIScrollView *scrollView; // The inputView that is always visible, right below the content. @@ -73,7 +92,10 @@ @property (nonatomic, strong, readonly) RDRKeyboardInputView *inputView; // Designated initializer -- (instancetype)initWithScrollView:(UIScrollView *)scrollView; +- (instancetype)initWithScrollView:(UIScrollView *)scrollView + delegate:(id)delegate + leftButton:(UIButton *)leftButtonOrNil + rightButton:(UIButton *)rightButtonOrNil; - (void)showKeyboard; - (void)hideKeyboard; diff --git a/RDRStickyKeyboardView/RDRStickyKeyboardView.m b/RDRStickyKeyboardView/RDRStickyKeyboardView.m index babee1d..9da3c30 100644 --- a/RDRStickyKeyboardView/RDRStickyKeyboardView.m +++ b/RDRStickyKeyboardView/RDRStickyKeyboardView.m @@ -207,11 +207,14 @@ static inline UIViewAnimationOptions RDRAnimationOptionsForCurve(UIViewAnimation #define RDR_KEYBOARD_INPUT_VIEW_MARGIN_BUTTONS_VERTICAL 7 @interface RDRKeyboardInputView () { - UITextView *_textView; - UIButton *_leftButton; - UIButton *_rightButton; + } +@property (nonatomic, strong) UIButton *leftButton; +@property (nonatomic, strong) UIButton *rightButton; +@property (nonatomic, strong) UITextView *textView; + + @property (nonatomic, strong, readonly) UIToolbar *toolbar; @end @@ -231,6 +234,17 @@ - (id)initWithFrame:(CGRect)frame return self; } +-(id)initWithLeftButton:(UIButton *)leftButtonOrNil rightButton:(UIButton *)rightButtonOrNil +{ + if (self = [super init]) { + self.leftButton = leftButtonOrNil; + self.rightButton = rightButtonOrNil; + [self _setupSubviews]; + } + + return self; +} + #pragma mark - NSCoding - (id)initWithCoder:(NSCoder *)decoder @@ -280,11 +294,7 @@ - (UIButton *)leftButton return _leftButton; } - _leftButton = [UIButton buttonWithType:UIButtonTypeSystem]; - _leftButton.titleLabel.font = [UIFont systemFontOfSize:15.0f]; - - [_leftButton setTitle:NSLocalizedString(@"Other", nil) - forState:UIControlStateNormal]; + _leftButton = [[UIButton alloc] initWithFrame:CGRectZero]; return _leftButton; } @@ -295,11 +305,7 @@ - (UIButton *)rightButton return _rightButton; } - _rightButton = [UIButton buttonWithType:UIButtonTypeSystem]; - _rightButton.titleLabel.font = [UIFont systemFontOfSize:14.0f]; - - [_rightButton setTitle:NSLocalizedString(@"Send", nil) - forState:UIControlStateNormal]; + _rightButton = [[UIButton alloc] initWithFrame:CGRectZero]; return _rightButton; } @@ -481,6 +487,9 @@ @interface RDRStickyKeyboardView () { @property (nonatomic, strong, readonly) RDRKeyboardInputView *inputViewKeyboard; +@property(nonatomic, strong) UIButton * leftButton; +@property(nonatomic, strong) UIButton * rightButton; + @end @implementation RDRStickyKeyboardView @@ -488,9 +497,16 @@ @implementation RDRStickyKeyboardView #pragma mark - Lifecycle - (instancetype)initWithScrollView:(UIScrollView *)scrollView + delegate:(id)delegate + leftButton:(UIButton *)leftButtonOrNil + rightButton:(UIButton *)rightButtonOrNil { if (self = [super init]) { + self.leftButton = leftButtonOrNil; + self.rightButton = rightButtonOrNil; + self.delegate = delegate; + _scrollView = scrollView; _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth| UIViewAutoresizingFlexibleHeight; @@ -500,6 +516,9 @@ - (instancetype)initWithScrollView:(UIScrollView *)scrollView [self _setupSubviews]; [self _registerForNotifications]; + //setup the actions for the buttons + [self _setupKeyboardButtonActions]; + } return self; @@ -524,7 +543,7 @@ - (RDRKeyboardInputView *)inputViewKeyboard - (RDRKeyboardInputView *)inputViewScrollView { if (!_inputViewScrollView) { - _inputViewScrollView = [RDRKeyboardInputView new]; + _inputViewScrollView = [[RDRKeyboardInputView alloc] initWithLeftButton:self.leftButton rightButton:self.rightButton]; } return _inputViewScrollView; @@ -535,6 +554,11 @@ - (RDRKeyboardInputView *)inputView return self.inputViewKeyboard; } +-(UITextView *)textView +{ + return self.inputViewScrollView.textView; +} + #pragma mark - Overrides - (void)willMoveToSuperview:(UIView *)newSuperview @@ -563,6 +587,32 @@ - (void)hideKeyboard #pragma mark - Private +-(void)_setupKeyboardButtonActions +{ +// inputViewScrollView + [self.inputViewScrollView.leftButton addTarget:self action:@selector(onLeftKeyboardButtonTap:) forControlEvents:UIControlEventTouchUpInside]; + + [self.inputViewScrollView.rightButton addTarget:self action:@selector(onRightKeyboardButtonTap:) forControlEvents:UIControlEventTouchUpInside]; + + +// inputViewKeyboard + [self.inputViewKeyboard.leftButton addTarget:self action:@selector(onLeftKeyboardButtonTap:) forControlEvents:UIControlEventTouchUpInside]; + + [self.inputViewKeyboard.rightButton addTarget:self action:@selector(onRightKeyboardButtonTap:) forControlEvents:UIControlEventTouchUpInside]; +} + +#pragma mark - Keyboard Actions handlers + +-(IBAction)onLeftKeyboardButtonTap:(id)sender +{ + [self.delegate didSelectLeftKeyboardButton]; +} + +-(IBAction)onRightKeyboardButtonTap:(id)sender +{ + [self.delegate didSelectRightKeyboardButton]; +} + - (void)_setupSubviews { // Add scrollview as subview diff --git a/RDRStickyKeyboardViewExample/RDRStickyKeyboardViewExample.xcodeproj/project.xcworkspace/xcshareddata/RDRStickyKeyboardViewExample.xccheckout b/RDRStickyKeyboardViewExample/RDRStickyKeyboardViewExample.xcodeproj/project.xcworkspace/xcshareddata/RDRStickyKeyboardViewExample.xccheckout deleted file mode 100644 index 100e72d..0000000 --- a/RDRStickyKeyboardViewExample/RDRStickyKeyboardViewExample.xcodeproj/project.xcworkspace/xcshareddata/RDRStickyKeyboardViewExample.xccheckout +++ /dev/null @@ -1,41 +0,0 @@ - - - - - IDESourceControlProjectFavoriteDictionaryKey - - IDESourceControlProjectIdentifier - 6D50DFF2-7F53-4815-B8D9-D508230D8DD0 - IDESourceControlProjectName - RDRStickyKeyboardViewExample - IDESourceControlProjectOriginsDictionary - - 22D61CD2-CCE9-48B9-BC5E-F28DFD18865E - ssh://github.com/datwelk/RDRStickyKeyboardView.git - - IDESourceControlProjectPath - RDRStickyKeyboardViewExample/RDRStickyKeyboardViewExample.xcodeproj/project.xcworkspace - IDESourceControlProjectRelativeInstallPathDictionary - - 22D61CD2-CCE9-48B9-BC5E-F28DFD18865E - ../../.. - - IDESourceControlProjectURL - ssh://github.com/datwelk/RDRStickyKeyboardView.git - IDESourceControlProjectVersion - 110 - IDESourceControlProjectWCCIdentifier - 22D61CD2-CCE9-48B9-BC5E-F28DFD18865E - IDESourceControlProjectWCConfigurations - - - IDESourceControlRepositoryExtensionIdentifierKey - public.vcs.git - IDESourceControlWCCIdentifierKey - 22D61CD2-CCE9-48B9-BC5E-F28DFD18865E - IDESourceControlWCCName - RDRStickyKeyboardView - - - - diff --git a/RDRStickyKeyboardViewExample/RDRStickyKeyboardViewExample/ViewController.m b/RDRStickyKeyboardViewExample/RDRStickyKeyboardViewExample/ViewController.m index 048fa6e..9ddbf77 100644 --- a/RDRStickyKeyboardViewExample/RDRStickyKeyboardViewExample/ViewController.m +++ b/RDRStickyKeyboardViewExample/RDRStickyKeyboardViewExample/ViewController.m @@ -29,7 +29,7 @@ static NSString * const CellIdentifier = @"cell"; -@interface ViewController () +@interface ViewController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) RDRStickyKeyboardView *contentWrapper; @@ -60,11 +60,20 @@ - (void)_setupSubviews |UIViewAutoresizingFlexibleHeight; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier]; - + + //configure the stickyKeyboardView + UIButton * rightButton = [self newButtonWithTitle:NSLocalizedString(@"Send", nil)]; + UIButton * leftButton = [self newButtonWithTitle:NSLocalizedString(@"Other", nil)]; + + RDRStickyKeyboardView * stickyKeyboardView = [[RDRStickyKeyboardView alloc] initWithScrollView:self.tableView delegate:self leftButton:leftButton rightButton:rightButton]; + + stickyKeyboardView.frame = self.view.bounds; + stickyKeyboardView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; + stickyKeyboardView.textView.delegate = self; + // Setup wrapper - self.contentWrapper = [[RDRStickyKeyboardView alloc] initWithScrollView:self.tableView]; - self.contentWrapper.frame = self.view.bounds; - self.contentWrapper.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; + self.contentWrapper = stickyKeyboardView; + [self.view addSubview:self.contentWrapper]; } @@ -94,4 +103,57 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView return 1; } +#pragma mark - RDRStickyKeyboardViewDelegate + +-(void)didSelectLeftKeyboardButton +{ + [self onCancelTap:nil]; +} +-(void)didSelectRightKeyboardButton +{ + [self onDoneTap:nil]; +} + +#pragma mark - Actions + +-(IBAction)onDoneTap:(id)sender +{ + NSLog(@"Send tapped!!"); +} + +-(IBAction)onCancelTap:(id)sender +{ + NSLog(@"Other tapped!!"); +} + +#pragma mark - UITextViewDelegate + +- (void)textViewDidBeginEditing:(UITextView *)textView{ + NSLog(@"textViewDidBeginEditing!!.."); +} + + +- (void)textViewDidEndEditing:(UITextView *)textView{ + NSLog(@"textViewDidEndEditing!!.."); + +} + +#pragma mark - Utilities + +- (UIButton *)newButtonWithTitle:(NSString *)title +{ + UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem]; + button.titleLabel.font = [UIFont systemFontOfSize:14.0f]; + + [button setTitle:title + forState:UIControlStateNormal]; + return button; +} + + +-(void)hideKeyboard +{ + [self.view endEditing:YES]; +} + @end diff --git a/README.md b/README.md index 37c23d9..7263add 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,39 @@ Check out the sample project to learn more. ```objectivec UITableView *tableView = ...; -RDRStickyKeyboardView *keyboardView = [[RDRStickyKeyboardView alloc] initWithScrollView:tableView]; -keyboardView.frame = self.view.bounds; -keyboardView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; -[self.view addSubview:keyboardView]; +//create your own buttons to show on the keyboard +UIButton * rightButton = ...; +UIButton * leftButton = ...; + +RDRStickyKeyboardView * stickyKeyboardView = [[RDRStickyKeyboardView alloc] initWithScrollView:tableView delegate:self leftButton:leftButton rightButton:rightButton]; + +stickyKeyboardView.frame = self.view.bounds; +stickyKeyboardView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; +//set delegate to receive left/right button actions +stickyKeyboardView.textView.delegate = self; + +[self.view addSubview:stickyKeyboardView]; ``` +## RDRStickyKeyboardViewDelegate +In order to catch when the user Taps on the left/right buttons of the keyboard, you must conform to the RDRStickyKeyboardViewDelegate protocol. + +```objectivec +-(void)didSelectLeftKeyboardButton +{ + //code for handling left keyboard button Tap +} +-(void)didSelectRightKeyboardButton +{ + //code for handling right keyboard button Tap +} +``` + +## Accessing the UITextView +You have readOnly access to the UITextView shown as a convenience property 'textView' of the RDRStickyKeyboardView class. +Use it to access text content as well as for implementing UITextViewDelegate if your code requires this (see demo project for more info on this). + + ## Requirements * ARC * iOS 7