Skip to content

Commit

Permalink
Add modifiers to override background color and button image
Browse files Browse the repository at this point in the history
  • Loading branch information
iovortex committed Jul 30, 2012
1 parent 9ed2bbe commit 6448003
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
3 changes: 3 additions & 0 deletions UIInputToolbarSample/Classes/UIInputToolbar/UIInputToolbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ extern NSString * const CHExpandingTextViewWillChangeHeightNotification;
@property (nonatomic) BOOL inputButtonShouldDisableForNoText;
@property (assign) NSObject<UIInputToolbarDelegate> *delegate;

@property (nonatomic, retain) UIImage *backgroundImage;
@property (nonatomic, retain) UIImage *inputButtonImage;

@end
36 changes: 30 additions & 6 deletions UIInputToolbarSample/Classes/UIInputToolbar/UIInputToolbar.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@

NSString * const CHExpandingTextViewWillChangeHeightNotification = @"CHExpandingTextViewWillChangeHeight";

@interface UIInputToolbar ()

@property (nonatomic, retain) UIButton *innerBarButton;

@end


@implementation UIInputToolbar

Expand All @@ -35,6 +41,9 @@ @implementation UIInputToolbar
@synthesize inputButton;
@synthesize inputButtonShouldDisableForNoText;
@synthesize delegate;
@synthesize backgroundImage;
@synthesize inputButtonImage=_inputButtonImage;
@synthesize innerBarButton;

-(void)inputButtonPressed
{
Expand All @@ -54,8 +63,7 @@ -(void)setupToolbar:(NSString *)buttonLabel
self.tintColor = [UIColor lightGrayColor];

/* Create custom send button*/
UIImage *buttonImage = [UIImage imageNamed:@"buttonbg.png"];
buttonImage = [buttonImage stretchableImageWithLeftCapWidth:floorf(buttonImage.size.width/2) topCapHeight:floorf(buttonImage.size.height/2)];
UIImage *stretchableButtonImage = [self.inputButtonImage stretchableImageWithLeftCapWidth:floorf(self.inputButtonImage.size.width/2) topCapHeight:floorf(self.inputButtonImage.size.height/2)];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.font = [UIFont boldSystemFontOfSize:15.0f];
Expand All @@ -64,11 +72,13 @@ -(void)setupToolbar:(NSString *)buttonLabel
button.contentStretch = CGRectMake(0.5, 0.5, 0, 0);
button.contentMode = UIViewContentModeScaleToFill;

[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:stretchableButtonImage forState:UIControlStateNormal];
[button setTitle:buttonLabel forState:UIControlStateNormal];
[button addTarget:self action:@selector(inputButtonPressed) forControlEvents:UIControlEventTouchDown];
[button sizeToFit];

self.innerBarButton = button;

self.inputButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.inputButton.customView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
/* Disable button initially */
Expand Down Expand Up @@ -102,6 +112,8 @@ -(void)setupToolbar:(NSString *)buttonLabel
-(id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
[self setInputButtonImage:[UIImage imageNamed:@"buttonbg.png"]];
[self setBackgroundImage:[UIImage imageNamed:@"toolbarbg.png"]];
[self setupToolbar:@"Send"];
}
return self;
Expand All @@ -118,9 +130,8 @@ -(id)init
- (void)drawRect:(CGRect)rect
{
/* Draw custon toolbar background */
UIImage *backgroundImage = [UIImage imageNamed:@"toolbarbg.png"];
backgroundImage = [backgroundImage stretchableImageWithLeftCapWidth:floorf(backgroundImage.size.width/2) topCapHeight:floorf(backgroundImage.size.height/2)];
[backgroundImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
UIImage *stretchableBackgroundImage = [self.backgroundImage stretchableImageWithLeftCapWidth:floorf(self.backgroundImage.size.width/2) topCapHeight:floorf(self.backgroundImage.size.height/2)];
[stretchableBackgroundImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

CGRect i = self.inputButton.customView.frame;
i.origin.y = self.frame.size.height - i.size.height - 7;
Expand Down Expand Up @@ -182,4 +193,17 @@ -(void)expandingTextViewDidChange:(UIExpandingTextView *)expandingTextView
}
}

-(void)setInputButtonImage:(UIImage *)inputButtonImage {
if (![_inputButtonImage isEqual:inputButtonImage]) {
[_inputButtonImage release];
_inputButtonImage = [inputButtonImage retain];

if (self.innerBarButton) {
UIImage *stretchableButtonImage = [_inputButtonImage stretchableImageWithLeftCapWidth:floorf(_inputButtonImage.size.width/2) topCapHeight:floorf(_inputButtonImage.size.height/2)];
[self.innerBarButton setBackgroundImage:stretchableButtonImage forState:UIControlStateNormal];
}
}
}


@end

0 comments on commit 6448003

Please sign in to comment.