// // JSMessageInputView.m // // Created by Jesse Squires on 2/12/13. // Copyright (c) 2013 Hexed Bits. All rights reserved. // // http://www.hexedbits.com // // // Largely based on work by Sam Soffes // https://github.com/soffes // // SSMessagesViewController // https://github.com/soffes/ssmessagesviewcontroller // // // The MIT License // Copyright (c) 2013 Jesse Squires // // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and // associated documentation files (the "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the // following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // #import "JSMessageInputView.h" #import "JSBubbleView.h" #import "NSString+JSMessagesView.h" @interface JSMessageInputView () - (void)setup; - (void)setupTextView; - (void)setupSendButton; @end @implementation JSMessageInputView #pragma mark - Initialization - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if(self) { [self setup]; } return self; } - (void)setup { self.image = [[UIImage imageNamed:@"input-bar"] resizableImageWithCapInsets:UIEdgeInsetsMake(19.0f, 3.0f, 19.0f, 3.0f)]; self.backgroundColor = [UIColor whiteColor]; self.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin); self.opaque = YES; self.userInteractionEnabled = YES; [self setupTextView]; [self setupSendButton]; } - (void)setupTextView { CGFloat width = ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) ? 246.0f : 690.0f; CGFloat height = [JSMessageInputView textViewLineHeight]; self.textView = [[UITextView alloc] initWithFrame:CGRectMake(6.0f, 3.0f, width, height)]; self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth; self.textView.backgroundColor = [UIColor whiteColor]; self.textView.scrollIndicatorInsets = UIEdgeInsetsMake(10.0f, 0.0f, 10.0f, 8.0f); self.textView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f); self.textView.scrollEnabled = YES; self.textView.scrollsToTop = NO; self.textView.userInteractionEnabled = YES; self.textView.font = [JSBubbleView font]; self.textView.textColor = [UIColor blackColor]; self.textView.backgroundColor = [UIColor whiteColor]; self.textView.keyboardAppearance = UIKeyboardAppearanceDefault; self.textView.keyboardType = UIKeyboardTypeDefault; self.textView.returnKeyType = UIReturnKeyDefault; [self addSubview:self.textView]; UIImageView *inputFieldBack = [[UIImageView alloc] initWithFrame:CGRectMake(self.textView.frame.origin.x - 1.0f, 0.0f, self.textView.frame.size.width + 2.0f, self.frame.size.height)]; inputFieldBack.image = [[UIImage imageNamed:@"input-field"] resizableImageWithCapInsets:UIEdgeInsetsMake(20.0f, 12.0f, 18.0f, 18.0f)]; inputFieldBack.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); inputFieldBack.backgroundColor = [UIColor clearColor]; [self addSubview:inputFieldBack]; } - (void)setupSendButton { self.sendButton = [UIButton buttonWithType:UIButtonTypeCustom]; self.sendButton.frame = CGRectMake(self.frame.size.width - 65.0f, 8.0f, 59.0f, 26.0f); self.sendButton.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin); UIEdgeInsets insets = UIEdgeInsetsMake(0.0f, 13.0f, 0.0f, 13.0f); UIImage *sendBack = [[UIImage imageNamed:@"send"] resizableImageWithCapInsets:insets]; UIImage *sendBackHighLighted = [[UIImage imageNamed:@"send-highlighted"] resizableImageWithCapInsets:insets]; [self.sendButton setBackgroundImage:sendBack forState:UIControlStateNormal]; [self.sendButton setBackgroundImage:sendBack forState:UIControlStateDisabled]; [self.sendButton setBackgroundImage:sendBackHighLighted forState:UIControlStateHighlighted]; NSString *title = NSLocalizedString(@"Send", nil); [self.sendButton setTitle:title forState:UIControlStateNormal]; [self.sendButton setTitle:title forState:UIControlStateHighlighted]; [self.sendButton setTitle:title forState:UIControlStateDisabled]; self.sendButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f]; UIColor *titleShadow = [UIColor colorWithRed:0.325f green:0.463f blue:0.675f alpha:1.0f]; [self.sendButton setTitleShadowColor:titleShadow forState:UIControlStateNormal]; [self.sendButton setTitleShadowColor:titleShadow forState:UIControlStateHighlighted]; self.sendButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f); [self.sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self.sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; [self.sendButton setTitleColor:[UIColor colorWithWhite:1.0f alpha:0.5f] forState:UIControlStateDisabled]; self.sendButton.enabled = NO; [self addSubview:self.sendButton]; } #pragma mark - Message input view - (void)adjustTextViewHeightBy:(CGFloat)changeInHeight { CGRect prevFrame = self.textView.frame; int numLines = MAX([JSBubbleView numberOfLinesForMessage:self.textView.text], [self.textView.text numberOfLines]); self.textView.frame = CGRectMake(prevFrame.origin.x, prevFrame.origin.y, prevFrame.size.width, prevFrame.size.height + changeInHeight); self.textView.contentInset = UIEdgeInsetsMake((numLines >= 6 ? 4.0f : 0.0f), 0.0f, (numLines >= 6 ? 4.0f : 0.0f), 0.0f); self.textView.scrollEnabled = (numLines >= 4); } + (CGFloat)textViewLineHeight { return 30.0f; // for fontSize 15.0f } + (CGFloat)maxLines { return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) ? 4.0f : 8.0f; } + (CGFloat)maxHeight { return ([JSMessageInputView maxLines] + 1.0f) * [JSMessageInputView textViewLineHeight]; } @end