Skip to content
This repository has been archived by the owner on Aug 24, 2019. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Nov 8, 2010
0 parents commit 0b7fc55
Show file tree
Hide file tree
Showing 21 changed files with 924 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
.DS_Store
build
*.mode1v3
*.pbxuser
*.perspectivev3
*.xcworkspace
xcuserdata
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "Demo/SSToolkit"]
path = Demo/SSToolkit
url = git://github.com/samsoffes/sstoolkit.git
24 changes: 24 additions & 0 deletions Classes/SSMessageTableViewCell.h
@@ -0,0 +1,24 @@
//
// SSMessageTableViewCell.h
// Messages
//
// Created by Sam Soffes on 3/10/10.
// Copyright 2010 Sam Soffes. All rights reserved.
//

typedef enum {
SSMessageTableViewCellMessageStyleGray = 0,
SSMessageTableViewCellMessageStyleGreen = 1
} SSMessageTableViewCellMessageStyle;

@class SSMessageTableViewCellBubbleView;

@interface SSMessageTableViewCell : UITableViewCell {

SSMessageTableViewCellBubbleView *bubbleView;
}

@property (nonatomic, copy) NSString *messageText;
@property (nonatomic, assign) SSMessageTableViewCellMessageStyle messageStyle;

@end
58 changes: 58 additions & 0 deletions Classes/SSMessageTableViewCell.m
@@ -0,0 +1,58 @@
//
// SSMessageTableViewCell.m
// Messages
//
// Created by Sam Soffes on 3/10/10.
// Copyright 2010 Sam Soffes. All rights reserved.
//

#import "SSMessageTableViewCell.h"
#import "SSMessageTableViewCellBubbleView.h"

@implementation SSMessageTableViewCell

#pragma mark NSObject

- (void)dealloc {
[bubbleView release];
[super dealloc];
}

#pragma mark UITableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.textLabel.hidden = YES;

bubbleView = [[SSMessageTableViewCellBubbleView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
bubbleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:bubbleView];
[self.contentView sendSubviewToBack:bubbleView];
}
return self;
}

#pragma mark Getters

- (SSMessageTableViewCellMessageStyle)messageStyle {
return bubbleView.messageStyle;
}


- (NSString *)messageText {
return bubbleView.messageText;
}

#pragma mark Setters

- (void)setMessageStyle:(SSMessageTableViewCellMessageStyle)aMessageStyle {
bubbleView.messageStyle = aMessageStyle;
}


- (void)setMessageText:(NSString *)text {
bubbleView.messageText = text;
}

@end
25 changes: 25 additions & 0 deletions Classes/SSMessageTableViewCellBubbleView.h
@@ -0,0 +1,25 @@
//
// SSMessageTableViewCellBubbleView.h
// Messages
//
// Created by Sam Soffes on 3/10/10.
// Copyright 2010 Sam Soffes. All rights reserved.
//

#import "SSMessageTableViewCell.h"

@interface SSMessageTableViewCellBubbleView : UIView {

NSString *messageText;
SSMessageTableViewCellMessageStyle messageStyle;
}

@property (nonatomic, copy) NSString *messageText;
@property (nonatomic, assign) SSMessageTableViewCellMessageStyle messageStyle;

+ (UIImage *)bubbleImageForMessageStyle:(SSMessageTableViewCellMessageStyle)aMessageStyle;
+ (CGSize)textSizeForText:(NSString *)text;
+ (CGSize)bubbleSizeForText:(NSString *)text;
+ (CGFloat)cellHeightForText:(NSString *)text;

@end
74 changes: 74 additions & 0 deletions Classes/SSMessageTableViewCellBubbleView.m
@@ -0,0 +1,74 @@
//
// SSMessageTableViewCellBubbleView.m
// Messages
//
// Created by Sam Soffes on 3/10/10.
// Copyright 2010 Sam Soffes. All rights reserved.
//

#import "SSMessageTableViewCellBubbleView.h"

#define kFont [UIFont systemFontOfSize:15.0]
static UILineBreakMode kLineBreakMode = UILineBreakModeWordWrap;
static CGFloat kMaxWidth = 223.0; // TODO: Make dynamic
static CGFloat kPaddingTop = 6.0;
static CGFloat kPaddingBottom = 8.0;
static CGFloat kMarginTop = 2.0;
static CGFloat kMarginBottom = 2.0;

@implementation SSMessageTableViewCellBubbleView

@synthesize messageText;
@synthesize messageStyle;

#pragma mark Class Methods

+ (UIImage *)bubbleImageForMessageStyle:(SSMessageTableViewCellMessageStyle)aMessageStyle {
UIImage *image;
if (aMessageStyle == SSMessageTableViewCellMessageStyleGreen) {
image = [[UIImage imageNamed:@"SSMessageTableViewCellBackgroundGreen.png"] stretchableImageWithLeftCapWidth:17 topCapHeight:14];
} else {
image = [[UIImage imageNamed:@"SSMessageTableViewCellBackgroundGray.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:14];
}
return image;
}


+ (CGSize)textSizeForText:(NSString *)text {
CGSize maxSize = CGSizeMake(kMaxWidth - 38.0, 1000.0);
return [text sizeWithFont:kFont constrainedToSize:maxSize lineBreakMode:kLineBreakMode];
}


+ (CGSize)bubbleSizeForText:(NSString *)text {
CGSize textSize = [self textSizeForText:text];
return CGSizeMake(textSize.width + 38.0, textSize.height + kPaddingTop + kPaddingBottom);
}


+ (CGFloat)cellHeightForText:(NSString *)text {
return [self bubbleSizeForText:text].height + kMarginTop + kMarginBottom;
}

#pragma mark UIView

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor colorWithRed:0.859 green:0.886 blue:0.929 alpha:1.0];
}
return self;
}


- (void)drawRect:(CGRect)frame {
UIImage *bubbleImage = [[self class] bubbleImageForMessageStyle:messageStyle];
CGSize bubbleSize = [[self class] bubbleSizeForText:messageText];
CGRect bubbleFrame = CGRectMake((messageStyle == SSMessageTableViewCellMessageStyleGreen ? self.frame.size.width - bubbleSize.width : 0.0), kMarginTop, bubbleSize.width, bubbleSize.height);
[bubbleImage drawInRect:bubbleFrame];

CGSize textSize = [[self class] textSizeForText:messageText];
CGRect textFrame = CGRectMake(((messageStyle == SSMessageTableViewCellMessageStyleGreen) ? (13.0 + bubbleFrame.origin.x) : 23.0), kPaddingTop + kMarginTop, textSize.width, textSize.height);
[messageText drawInRect:textFrame withFont:kFont lineBreakMode:kLineBreakMode alignment:(messageStyle == SSMessageTableViewCellMessageStyleGreen) ? UITextAlignmentRight : UITextAlignmentLeft];
}

@end
30 changes: 30 additions & 0 deletions Classes/SSMessagesViewController.h
@@ -0,0 +1,30 @@
//
// SSMessagesViewController.h
// Messages
//
// This is an abstract class for displaying a UI similar
// to Apple's SMS application. A subclass should
// override the messageStyleForRowAtIndexPath: and
// textForRowAtIndexPath: to customize this class.
//
// Created by Sam Soffes on 3/10/10.
// Copyright 2010 Sam Soffes. All rights reserved.
//

#import "SSMessageTableViewCell.h"

@class SSGradientView;

@interface SSMessagesViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate> {

UITableView *_tableView;
SSGradientView *_inputView;
UIButton *_sendButton;
}

@property (nonatomic, retain, readonly) UITableView *tableView;

- (SSMessageTableViewCellMessageStyle)messageStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSString *)textForRowAtIndexPath:(NSIndexPath *)indexPath;

@end
161 changes: 161 additions & 0 deletions Classes/SSMessagesViewController.m
@@ -0,0 +1,161 @@
//
// SSMessagesViewController.m
// Messages
//
// Created by Sam Soffes on 3/10/10.
// Copyright 2010 Sam Soffes. All rights reserved.
//

#import "SSMessagesViewController.h"
#import "SSMessageTableViewCell.h"
#import "SSMessageTableViewCellBubbleView.h"
#import <SSToolkit/SSGradientView.h>
#import <SSToolkit/SSTextField.h>

CGFloat kInputHeight = 40.0;

@implementation SSMessagesViewController

@synthesize tableView = _tableView;

#pragma mark NSObject

- (id)init {
return [self initWithNibName:nil bundle:nil];
}


- (void)dealloc {
[_tableView release];
[_inputView release];
[_sendButton release];
[super dealloc];
}


#pragma mark UITableViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nil bundle:nil]) {
self.view.backgroundColor = [UIColor colorWithRed:0.859 green:0.886 blue:0.929 alpha:1.0];

// Table view
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height - kInputHeight) style:UITableViewStylePlain];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_tableView.backgroundColor = self.view.backgroundColor;
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.separatorColor = self.view.backgroundColor;
[self.view addSubview:_tableView];

// Input
_inputView = [[SSGradientView alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - kInputHeight, self.view.frame.size.width, kInputHeight)];
_inputView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_inputView.hasBottomBorder = NO;
_inputView.topBorderColor = [UIColor colorWithRed:0.733 green:0.741 blue:0.745 alpha:1.0];
_inputView.topColor = [UIColor colorWithRed:0.914 green:0.922 blue:0.929 alpha:1.0];
_inputView.bottomColor = [UIColor colorWithRed:0.765 green:0.773 blue:0.788 alpha:1.0];
[self.view addSubview:_inputView];

// Text field
SSTextField *textField = [[SSTextField alloc] initWithFrame:CGRectMake(6.0, 8.0, self.view.frame.size.width - 75.0, 27.0)];
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
textField.background = [[UIImage imageNamed:@"SSMessagesViewControllerTextFieldBackground.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:0];
textField.delegate = self;
textField.font = [UIFont systemFontOfSize:15.0];
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.textInsets = UIEdgeInsetsMake(0.0, 12.0, 0.0, 12.0);
[_inputView addSubview:textField];
[textField release];

// Send button
_sendButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
_sendButton.frame = CGRectMake(self.view.frame.size.width - 65.0, 8.0, 59.0, 27.0);
_sendButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
_sendButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
_sendButton.titleLabel.shadowOffset = CGSizeMake(0.0, -1.0);
[_sendButton setBackgroundImage:[[UIImage imageNamed:@"SSMessagesViewControllerSendButtonBackground.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:0] forState:UIControlStateNormal];
[_sendButton setTitle:@"Send" forState:UIControlStateNormal];
[_sendButton setTitleColor:[UIColor colorWithWhite:1.0 alpha:0.4] forState:UIControlStateNormal];
[_sendButton setTitleShadowColor:[UIColor colorWithRed:0.325 green:0.463 blue:0.675 alpha:1.0] forState:UIControlStateNormal];
[_inputView addSubview:_sendButton];
}
return self;
}


#pragma mark SSMessagesViewController

// This method is intended to be overridden by subclasses
- (SSMessageTableViewCellMessageStyle)messageStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return SSMessageTableViewCellMessageStyleGray;
}


// This method is intended to be overridden by subclasses
- (NSString *)textForRowAtIndexPath:(NSIndexPath *)indexPath {
return nil;
}


#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"cellIdentifier";

SSMessageTableViewCell *cell = (SSMessageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[SSMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}

cell.messageStyle = [self messageStyleForRowAtIndexPath:indexPath];
cell.messageText = [self textForRowAtIndexPath:indexPath];

return cell;
}


#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [SSMessageTableViewCellBubbleView cellHeightForText:[self textForRowAtIndexPath:indexPath]];
}


#pragma mark UITextFieldDelegate

- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:@"beginEditing" context:_inputView];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3];
_tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, 216.0, 0.0);
_tableView.scrollIndicatorInsets = _tableView.contentInset;
_inputView.frame = CGRectMake(0.0, 160.0, self.view.frame.size.width, kInputHeight);
[_sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[UIView commitAnimations];
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
[UIView beginAnimations:@"endEditing" context:_inputView];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3];
_tableView.contentInset = UIEdgeInsetsZero;
_tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
_inputView.frame = CGRectMake(0.0, _tableView.frame.size.height, self.view.frame.size.width, kInputHeight);
[_sendButton setTitleColor:[UIColor colorWithWhite:1.0 alpha:0.4] forState:UIControlStateNormal];
[UIView commitAnimations];
}

@end

0 comments on commit 0b7fc55

Please sign in to comment.