From 6448003f14a241d42543c81d09983e909a10ef8b Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Mon, 30 Jul 2012 12:39:09 -0500 Subject: [PATCH] Add modifiers to override background color and button image --- .../Classes/UIInputToolbar/UIInputToolbar.h | 3 ++ .../Classes/UIInputToolbar/UIInputToolbar.m | 36 +++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/UIInputToolbarSample/Classes/UIInputToolbar/UIInputToolbar.h b/UIInputToolbarSample/Classes/UIInputToolbar/UIInputToolbar.h index 06e84a1..f298f97 100644 --- a/UIInputToolbarSample/Classes/UIInputToolbar/UIInputToolbar.h +++ b/UIInputToolbarSample/Classes/UIInputToolbar/UIInputToolbar.h @@ -52,4 +52,7 @@ extern NSString * const CHExpandingTextViewWillChangeHeightNotification; @property (nonatomic) BOOL inputButtonShouldDisableForNoText; @property (assign) NSObject *delegate; +@property (nonatomic, retain) UIImage *backgroundImage; +@property (nonatomic, retain) UIImage *inputButtonImage; + @end diff --git a/UIInputToolbarSample/Classes/UIInputToolbar/UIInputToolbar.m b/UIInputToolbarSample/Classes/UIInputToolbar/UIInputToolbar.m index efa7130..4eddeed 100644 --- a/UIInputToolbarSample/Classes/UIInputToolbar/UIInputToolbar.m +++ b/UIInputToolbarSample/Classes/UIInputToolbar/UIInputToolbar.m @@ -27,6 +27,12 @@ NSString * const CHExpandingTextViewWillChangeHeightNotification = @"CHExpandingTextViewWillChangeHeight"; +@interface UIInputToolbar () + +@property (nonatomic, retain) UIButton *innerBarButton; + +@end + @implementation UIInputToolbar @@ -35,6 +41,9 @@ @implementation UIInputToolbar @synthesize inputButton; @synthesize inputButtonShouldDisableForNoText; @synthesize delegate; +@synthesize backgroundImage; +@synthesize inputButtonImage=_inputButtonImage; +@synthesize innerBarButton; -(void)inputButtonPressed { @@ -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]; @@ -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 */ @@ -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; @@ -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; @@ -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