Skip to content
This repository was archived by the owner on Oct 19, 2023. It is now read-only.

Commit febdf11

Browse files
committed
Added docs, and a few minor updates.
1 parent dc6db81 commit febdf11

File tree

8 files changed

+120
-165
lines changed

8 files changed

+120
-165
lines changed

RFKeyboardToolbar/RFKeyboardToolbar.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,41 @@
66
//
77

88
#import <UIKit/UIKit.h>
9+
#include <AvailabilityMacros.h>
10+
11+
#import "RFToolbarButton.h"
912

1013
@interface RFKeyboardToolbar : UIView
1114

15+
/**
16+
* The buttons of the toolbar.
17+
*/
1218
@property (nonatomic, strong) NSArray *buttons;
1319

14-
+ (instancetype)toolbarViewWithButtons:(NSArray *)buttons;
20+
/**
21+
* Creates a new toolbar.
22+
*
23+
* @param buttons The buttons to draw in the view.
24+
*
25+
* @return A RFKeyboardToolbar.
26+
*/
27+
+ (instancetype)toolbarWithButtons:(NSArray *)buttons;
28+
29+
/**
30+
* Creates a new toolbar.
31+
*
32+
* @param buttons The buttons to draw in the view.
33+
*
34+
* @return A RFKeyboardToolbar.
35+
*/
36+
+ (instancetype)toolbarViewWithButtons:(NSArray *)buttons DEPRECATED_MSG_ATTRIBUTE("This will still work, but there's a shorter method available, toolbarWithButtons:");
1537

38+
/**
39+
*
40+
*
41+
* @param buttons Sets the buttons for the toolbar.
42+
* @param animated Whether or not it should be animated.
43+
*/
1644
- (void)setButtons:(NSArray *)buttons animated:(BOOL)animated;
1745

1846
@end

RFKeyboardToolbar/RFKeyboardToolbar.m

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,39 @@
66
//
77

88
#import "RFKeyboardToolbar.h"
9-
#import "RFToolbarButton.h"
109

1110
@interface RFKeyboardToolbar ()
1211

12+
/**
13+
* The toolbar view.
14+
*/
1315
@property (nonatomic,strong) UIView *toolbarView;
16+
/**
17+
* The scroll view that's faked to look like a toolbar.
18+
*/
1419
@property (nonatomic,strong) UIScrollView *scrollView;
20+
/**
21+
* The fake top border to replicate the toolbar.
22+
*/
1523
@property (nonatomic,strong) CALayer *topBorder;
1624

1725
@end
1826

1927
@implementation RFKeyboardToolbar
2028

29+
+ (instancetype)toolbarWithButtons:(NSArray *)buttons {
30+
return [[RFKeyboardToolbar alloc] initWithButtons:buttons];
31+
}
32+
2133
+ (instancetype)toolbarViewWithButtons:(NSArray *)buttons {
2234
return [[RFKeyboardToolbar alloc] initWithButtons:buttons];
2335
}
2436

25-
- (id)initWithButtons:(NSArray*)buttons {
37+
- (id)initWithButtons:(NSArray *)buttons {
2638
self = [super initWithFrame:CGRectMake(0, 0, self.window.rootViewController.view.bounds.size.width, 40)];
2739
if (self) {
2840
_buttons = [buttons copy];
41+
2942
self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
3043
[self addSubview:[self inputAccessoryView]];
3144
}
@@ -39,7 +52,7 @@ - (void)layoutSubviews {
3952
_topBorder.frame = frame;
4053
}
4154

42-
- (UIView*)inputAccessoryView {
55+
- (UIView *)inputAccessoryView {
4356
_toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)];
4457
_toolbarView.backgroundColor = [UIColor colorWithWhite:0.973 alpha:1.0];
4558
_toolbarView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
@@ -54,7 +67,7 @@ - (UIView*)inputAccessoryView {
5467
return _toolbarView;
5568
}
5669

57-
- (UIScrollView*)fakeToolbar {
70+
- (UIScrollView *)fakeToolbar {
5871
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)];
5972
_scrollView.backgroundColor = [UIColor clearColor];
6073
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
@@ -66,8 +79,7 @@ - (UIScrollView*)fakeToolbar {
6679
return _scrollView;
6780
}
6881

69-
- (void)addButtons
70-
{
82+
- (void)addButtons {
7183
NSUInteger index = 0;
7284
NSUInteger originX = 8;
7385

@@ -88,17 +100,14 @@ - (void)addButtons
88100
_scrollView.contentSize = contentSize;
89101
}
90102

91-
- (void)setButtons:(NSArray*)buttons
92-
{
103+
- (void)setButtons:(NSArray *)buttons {
93104
[_buttons makeObjectsPerformSelector:@selector(removeFromSuperview)];
94105
_buttons = [buttons copy];
95106
[self addButtons];
96107
}
97108

98-
- (void)setButtons:(NSArray *)buttons animated:(BOOL)animated
99-
{
100-
if (animated == NO)
101-
{
109+
- (void)setButtons:(NSArray *)buttons animated:(BOOL)animated {
110+
if (!animated) {
102111
self.buttons = buttons;
103112
return;
104113
}
@@ -113,6 +122,7 @@ - (void)setButtons:(NSArray *)buttons animated:(BOOL)animated
113122
NSUInteger originX = 8;
114123
NSUInteger index = 0;
115124
NSMutableArray *buttonFrames = [NSMutableArray arrayWithCapacity:_buttons.count];
125+
116126
for (RFToolbarButton *button in _buttons) {
117127
CGRect frame = CGRectMake(originX, 0, button.frame.size.width, button.frame.size.height);
118128
[buttonFrames addObject:[NSValue valueWithCGRect:frame]];
@@ -123,8 +133,9 @@ - (void)setButtons:(NSArray *)buttons animated:(BOOL)animated
123133

124134
CGSize contentSize = _scrollView.contentSize;
125135
contentSize.width = originX - 8;
126-
if (contentSize.width > _scrollView.contentSize.width)
136+
if (contentSize.width > _scrollView.contentSize.width) {
127137
_scrollView.contentSize = contentSize;
138+
}
128139

129140
// make added buttons appear from the right
130141
[addButtons enumerateObjectsUsingBlock:^(RFToolbarButton *button, BOOL *stop) {

RFKeyboardToolbar/RFToolbarButton.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,39 @@
77

88
#import <UIKit/UIKit.h>
99

10+
/**
11+
* The block used for each button.
12+
*/
1013
typedef void (^eventHandlerBlock)();
1114

1215
@interface RFToolbarButton : UIButton
1316

17+
/**
18+
* Creates a new RFToolbarButton.
19+
*
20+
* @param title The string to show on the button.
21+
*
22+
* @return A new button.
23+
*/
1424
+ (instancetype)buttonWithTitle:(NSString *)title;
1525

26+
/**
27+
* Creates a new RFToolbarButton.
28+
*
29+
* @param title The string to show on the button.
30+
* @param eventHandler The event handler block.
31+
* @param controlEvent The type of event.
32+
*
33+
* @return A new button.
34+
*/
35+
+ (instancetype)buttonWithTitle:(NSString *)title andEventHandler:(eventHandlerBlock)eventHandler forControlEvents:(UIControlEvents)controlEvent;
36+
37+
/**
38+
* Adds the event handler for the button.
39+
*
40+
* @param eventHandler The event handler block.
41+
* @param controlEvent The type of event.
42+
*/
1643
- (void)addEventHandler:(eventHandlerBlock)eventHandler forControlEvents:(UIControlEvents)controlEvent;
1744

1845
@end

RFKeyboardToolbar/RFToolbarButton.m

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99

1010
@interface RFToolbarButton ()
1111

12+
/**
13+
* The button's title.
14+
*/
1215
@property (nonatomic, strong) NSString *title;
16+
17+
/**
18+
* The button's event block.
19+
*/
1320
@property (nonatomic, copy) eventHandlerBlock buttonPressBlock;
1421

1522
@end
@@ -20,17 +27,22 @@ + (instancetype)buttonWithTitle:(NSString *)title {
2027
return [[self alloc] initWithTitle:title];
2128
}
2229

30+
+ (instancetype)buttonWithTitle:(NSString *)title andEventHandler:(eventHandlerBlock)eventHandler forControlEvents:(UIControlEvents)controlEvent {
31+
RFToolbarButton *newButton = [RFToolbarButton buttonWithTitle:title];
32+
[newButton addEventHandler:eventHandler forControlEvents:controlEvent];
33+
34+
return newButton;
35+
}
36+
2337
- (id)initWithTitle:(NSString *)title {
2438
_title = title;
2539
return [self init];
2640
}
2741

28-
- (id)init
29-
{
42+
- (id)init {
3043
CGSize sizeOfText = [self.title sizeWithAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14.f]}];
3144

32-
self = [super initWithFrame:CGRectMake(0, 0, sizeOfText.width + 18.104, sizeOfText.height + 10.298)];
33-
if (self) {
45+
if (self = [super initWithFrame:CGRectMake(0, 0, sizeOfText.width + 18.104, sizeOfText.height + 10.298)]) {
3446
self.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.0];
3547

3648
self.layer.cornerRadius = 3.0f;

RFKeyboardToolbarDemo/RFKeyboardToolbar/AppDelegate.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
@implementation AppDelegate
1212

13-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
14-
{
13+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
1514
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
1615

1716
ViewController *viewController = [[ViewController alloc] init];

RFKeyboardToolbarDemo/RFKeyboardToolbar/ViewController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#import <UIKit/UIKit.h>
1010

11+
@class RFKeyboardToolbar;
12+
1113
@interface ViewController : UIViewController <UITextViewDelegate>
1214

1315
@end

RFKeyboardToolbarDemo/RFKeyboardToolbar/ViewController.m

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@
88

99
#import "ViewController.h"
1010
#import "RFKeyboardToolbar.h"
11-
#import "RFToolbarButton.h"
1211

1312
@interface ViewController ()
1413

15-
@property (nonatomic,strong) UITextView *textView;
14+
@property (strong, nonatomic) UITextView *textView;
15+
@property (strong, nonatomic) RFKeyboardToolbar *keyboardToolbar;
1616

1717
@end
1818

1919
@implementation UITextView (InsertWord)
2020

21-
- (void)insertWord:(NSString*)word
22-
{
21+
- (void)insertWord:(NSString*)word {
2322
// insert a word into the field, adding a space before and/or after it as necessary
2423
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
2524
NSRange selectedRange = self.selectedRange;
@@ -36,9 +35,6 @@ - (void)insertWord:(NSString*)word
3635
@end
3736

3837
@implementation ViewController
39-
{
40-
RFKeyboardToolbar *keyboardToolbar;
41-
}
4238

4339
- (void)viewDidLoad
4440
{
@@ -48,21 +44,27 @@ - (void)viewDidLoad
4844

4945
_textView = [[UITextView alloc] initWithFrame:self.view.bounds];
5046

51-
RFToolbarButton *exampleButton = [RFToolbarButton buttonWithTitle:@"Example"];
47+
NSMutableArray *buttons = NSMutableArray.array;
5248

53-
[exampleButton addEventHandler:^{
54-
[_textView insertText:@"You pressed a button!"];
55-
} forControlEvents:UIControlEventTouchUpInside];
49+
NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
50+
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
5651

57-
keyboardToolbar = [RFKeyboardToolbar toolbarViewWithButtons:@[exampleButton]];
58-
_textView.inputAccessoryView = keyboardToolbar;
52+
for (int i = 1; i <= 20; i++) {
53+
RFToolbarButton *button = [RFToolbarButton buttonWithTitle:[numberFormatter stringFromNumber:@(i)] andEventHandler:^{
54+
[_textView insertText:@"You pressed a button!"];
55+
} forControlEvents:UIControlEventTouchUpInside];
56+
57+
[buttons addObject:button];
58+
}
59+
60+
_keyboardToolbar = [RFKeyboardToolbar toolbarWithButtons:buttons];
61+
_textView.inputAccessoryView = _keyboardToolbar;
5962
_textView.delegate = self;
6063

6164
[self.view addSubview:_textView];
6265
}
6366

64-
- (void)textViewDidChange:(UITextView *)textView
65-
{
67+
- (void)textViewDidChange:(UITextView *)textView {
6668
// count words
6769
NSArray *allWords = [textView.text.lowercaseString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
6870
NSCountedSet *words = [NSCountedSet new];
@@ -73,7 +75,7 @@ - (void)textViewDidChange:(UITextView *)textView
7375
}
7476

7577
// dictionary of existing words => buttons
76-
NSDictionary *oldButtons = [NSDictionary dictionaryWithObjects:keyboardToolbar.buttons forKeys:[keyboardToolbar.buttons valueForKey:@"title"]];
78+
NSDictionary *oldButtons = [NSDictionary dictionaryWithObjects:_keyboardToolbar.buttons forKeys:[_keyboardToolbar.buttons valueForKey:@"title"]];
7779

7880
// create new buttons
7981
NSMutableArray *newButtons = [NSMutableArray arrayWithCapacity:words.count];
@@ -96,7 +98,7 @@ - (void)textViewDidChange:(UITextView *)textView
9698
if (count1 == count2) return [[obj1 title] compare:[obj2 title]];
9799
return count1 > count2 ? NSOrderedAscending : NSOrderedDescending;
98100
}];
99-
[keyboardToolbar setButtons:newButtons animated:YES];
101+
[_keyboardToolbar setButtons:newButtons animated:YES];
100102
}
101103

102104
@end

0 commit comments

Comments
 (0)