Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions JSTokenField/JSTokenButton.h → JSTokenButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,23 @@
// or implied, of James Addyman (JamSoft).
//


#import <UIKit/UIKit.h>
@class JSTokenField;

@interface JSTokenButton : UIButton <UIKeyInput> {

BOOL _toggled;

UIImage *_normalBg;
UIImage *_highlightedBg;

id _representedObject;

}
@class JSTokenField;

@property (nonatomic, getter=isToggled) BOOL toggled;

@property (nonatomic, retain) UIImage *normalBg;
@property (nonatomic, retain) UIImage *highlightedBg;
@interface JSTokenButton : UIButton <UIKeyInput>

@property (nonatomic, retain) id representedObject;
@property (nonatomic, retain) id identifier;
@property (nonatomic, getter=isActive) BOOL active;

@property (nonatomic, assign) JSTokenField *parentField;
@property (nonatomic, retain) UIImage *normalBackgroundImage, *highlightedBackgroundImage;


+ (JSTokenButton *)tokenWithLabel:(NSString *)labelText forIdentifier:(id)identifier;

+ (JSTokenButton *)tokenWithString:(NSString *)string representedObject:(id)obj;

@end
125 changes: 69 additions & 56 deletions JSTokenField/JSTokenButton.m → JSTokenButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,99 +26,112 @@
// or implied, of James Addyman (JamSoft).
//


#import "JSTokenButton.h"
#import "JSTokenField.h"
#import <QuartzCore/QuartzCore.h>


@implementation JSTokenButton

@synthesize toggled = _toggled;
@synthesize normalBg = _normalBg;
@synthesize highlightedBg = _highlightedBg;
@synthesize representedObject = _representedObject;
@synthesize parentField = _parentField;

+ (JSTokenButton *)tokenWithString:(NSString *)string representedObject:(id)obj
+ (JSTokenButton *)tokenWithLabel:(NSString *)labelText forIdentifier:(id)identifier
{
JSTokenButton *button = (JSTokenButton *)[self buttonWithType:UIButtonTypeCustom];
[button setNormalBg:[[UIImage imageNamed:@"tokenNormal.png"] stretchableImageWithLeftCapWidth:14 topCapHeight:0]];
[button setHighlightedBg:[[UIImage imageNamed:@"tokenHighlighted.png"] stretchableImageWithLeftCapWidth:14 topCapHeight:0]];
[button setAdjustsImageWhenHighlighted:NO];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[button titleLabel] setFont:[UIFont fontWithName:@"Helvetica Neue" size:15]];
[[button titleLabel] setLineBreakMode:UILineBreakModeTailTruncation];
[button setTitleEdgeInsets:UIEdgeInsetsMake(2, 10, 0, 10)];
JSTokenButton *token = (JSTokenButton *)[self buttonWithType:UIButtonTypeCustom];
token.identifier = identifier;
token.active = FALSE;

// Set the background appearance
token.adjustsImageWhenHighlighted = FALSE;
token.normalBackgroundImage = [[UIImage imageNamed:@"tokenNormal.png"] stretchableImageWithLeftCapWidth:14 topCapHeight:0];
token.highlightedBackgroundImage = [[UIImage imageNamed:@"tokenHighlighted.png"] stretchableImageWithLeftCapWidth:14 topCapHeight:0];

// Style the buttons appearance
[token setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[token.titleLabel setFont:[UIFont fontWithName:@"Helvetica Neue" size:15]];
[token.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[token setTitleEdgeInsets:UIEdgeInsetsMake(2, 10, 0, 10)];
[token setTitle:labelText forState:UIControlStateNormal];

[button setTitle:string forState:UIControlStateNormal];
// Adjust the tokens frame
[token sizeToFit];

[button sizeToFit];
CGRect frame = [button frame];
CGRect frame = [token frame];
frame.size.width += 20;
frame.size.height = 25;
[button setFrame:frame];
token.frame = frame;

[button setToggled:NO];
[token updateAppearance];

[button setRepresentedObject:obj];

return button;
return token;
}

- (void)setToggled:(BOOL)toggled
- (void)setActive:(BOOL)active
{
_toggled = toggled;
_active = active;

if (_toggled)
{
[self setBackgroundImage:self.highlightedBg forState:UIControlStateNormal];
[self updateAppearance];
}


- (void)updateAppearance
{
if([self isActive]) {
[self setBackgroundImage:self.highlightedBackgroundImage forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
else
{
[self setBackgroundImage:self.normalBg forState:UIControlStateNormal];
} else {
[self setBackgroundImage:self.normalBackgroundImage forState:UIControlStateNormal];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
}

- (void)dealloc

- (BOOL)canBecomeFirstResponder
{
self.representedObject = nil;
self.highlightedBg = nil;
self.normalBg = nil;
[super dealloc];
return YES;
}

- (BOOL)becomeFirstResponder {
BOOL superReturn = [super becomeFirstResponder];
if (superReturn) {
self.toggled = YES;
}
return superReturn;

- (BOOL)becomeFirstResponder
{
BOOL shouldBecomeFirstResponder = [super becomeFirstResponder];
if(shouldBecomeFirstResponder)
self.active = TRUE;

return shouldBecomeFirstResponder;
}

- (BOOL)resignFirstResponder {
BOOL superReturn = [super resignFirstResponder];
if (superReturn) {
self.toggled = NO;
}
return superReturn;

- (BOOL)resignFirstResponder
{
BOOL shouldResignFirstResponder = [super resignFirstResponder];
if(shouldResignFirstResponder)
self.active = FALSE;

return shouldResignFirstResponder;
}



#pragma mark - UIKeyInput
- (void)deleteBackward {
[_parentField removeTokenForString:[self titleForState:UIControlStateNormal]];

- (void)deleteBackward
{
[self.parentField becomeFirstResponder];
[self.parentField removeTokenForIdentifier:self.identifier];
}

- (BOOL)hasText {

- (BOOL)hasText
{
return NO;
}
- (void)insertText:(NSString *)text {
return;
}


- (BOOL)canBecomeFirstResponder {
return YES;
- (void)insertText:(NSString *)text
{
}



@end
60 changes: 26 additions & 34 deletions JSTokenField/JSTokenField.h → JSTokenField.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,38 @@

#import <UIKit/UIKit.h>

@class JSTokenButton;
@protocol JSTokenFieldDelegate;

extern NSString *const JSTokenFieldFrameDidChangeNotification;
extern NSString *const JSTokenFieldNewFrameKey;
extern NSString *const JSTokenFieldOldFrameKey;
extern NSString *const JSDeletedTokenKey;

@interface JSTokenField : UIView <UITextFieldDelegate> {

NSMutableArray *_tokens;

UITextField *_textField;

id <JSTokenFieldDelegate> _delegate;

JSTokenButton *_deletedToken;

UILabel *_label;
}

@property (nonatomic, readonly) UITextField *textField;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, readonly, copy) NSMutableArray *tokens;
@property (nonatomic, assign) id <JSTokenFieldDelegate> delegate;

- (void)addTokenWithTitle:(NSString *)string representedObject:(id)obj;
- (void)removeTokenForString:(NSString *)string;
- (void)removeTokenWithRepresentedObject:(id)representedObject;
@class JSTokenField;

@end

@protocol JSTokenFieldDelegate <NSObject>

@optional

- (void)tokenField:(JSTokenField *)tokenField didAddToken:(NSString *)title representedObject:(id)obj;
- (void)tokenField:(JSTokenField *)tokenField didRemoveToken:(NSString *)title representedObject:(id)obj;
- (NSArray *)tokenField:(JSTokenField *)tokenField tokenIdentifiersForString:(NSString *)untokenizedText;
- (NSString *)tokenField:(JSTokenField *)tokenField labelForIdentifier:(id)identifier;

- (BOOL)tokenFieldShouldReturn:(JSTokenField *)tokenField;
- (void)tokenField:(JSTokenField *)tokenField didAddTokenWithIdentifier:(id)identifier;
- (void)tokenField:(JSTokenField *)tokenField didRemoveTokenWithIdentifier:(id)identifier;
- (void)tokenFieldDidEndEditing:(JSTokenField *)tokenField;

@end



@interface JSTokenField : UIView <UITextFieldDelegate>

@property (nonatomic, assign) id <JSTokenFieldDelegate> delegate;
@property (nonatomic, readonly, retain) UILabel *label;
@property (nonatomic, readonly, retain) UITextField *textField;

@property (nonatomic, assign) UIEdgeInsets contentInsets;
@property (nonatomic, assign) CGSize tokenPadding;

- (NSArray *)allTokens;
- (void)addTokenIdentifiers:(NSArray *)tokenIdentifiers;
- (void)removeAllTokens;

- (void)addTokenWithLabel:(NSString *)labelText forIdentifier:(id)identifier;
- (void)removeTokenForIdentifier:(id)identifier;

@end

Loading