Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some changes #31

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
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
22 changes: 10 additions & 12 deletions JSTokenField/JSTokenButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,26 @@
//

#import <UIKit/UIKit.h>

@class JSTokenField;

@interface JSTokenButton : UIButton <UIKeyInput> {
@protocol JSTokenButtonCustomView <NSObject>
@optional
- (void)buttonStateChanged:(BOOL)selected;
@end

BOOL _toggled;

UIImage *_normalBg;
UIImage *_highlightedBg;

@interface JSTokenButton : UIButton <UIKeyInput> {
id _representedObject;
id _value;
}

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

@property (nonatomic, retain) UIImage *normalBg;
@property (nonatomic, retain) UIImage *highlightedBg;

@property (nonatomic, retain) id representedObject;
@property (nonatomic, retain) id value;

@property (nonatomic, retain) UIView<JSTokenButtonCustomView> *customView;
@property (nonatomic, assign) JSTokenField *parentField;

+ (JSTokenButton *)tokenWithString:(NSString *)string representedObject:(id)obj;
+ (JSTokenButton *)tokenWithView:(UIView<JSTokenButtonCustomView> *)view representedObject:(id)obj;

@end
82 changes: 46 additions & 36 deletions JSTokenField/JSTokenButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,24 @@

@implementation JSTokenButton

@synthesize toggled = _toggled;
@synthesize normalBg = _normalBg;
@synthesize highlightedBg = _highlightedBg;
@synthesize value = _value;
@synthesize representedObject = _representedObject;
@synthesize parentField = _parentField;
@synthesize customView = _customView;

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

+ (JSTokenButton *)tokenWithString:(NSString *)string representedObject:(id)obj {
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 setBackgroundImage:[[UIImage imageNamed:@"tokenNormal.png"] stretchableImageWithLeftCapWidth:14 topCapHeight:0]
forState:UIControlStateNormal];
[button setBackgroundImage:[[UIImage imageNamed:@"tokenHighlighted.png"] stretchableImageWithLeftCapWidth:14 topCapHeight:0]
forState:UIControlStateSelected];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[button setAdjustsImageWhenHighlighted:NO];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[button titleLabel] setFont:[UIFont fontWithName:@"Helvetica Neue" size:15]];
[[button titleLabel] setLineBreakMode:UILineBreakModeTailTruncation];
[[button titleLabel] setFont:[UIFont systemFontOfSize:15]];
[[button titleLabel] setLineBreakMode:NSLineBreakByTruncatingTail];
[button setTitleEdgeInsets:UIEdgeInsetsMake(2, 10, 0, 10)];

[button setTitle:string forState:UIControlStateNormal];
Expand All @@ -57,69 +60,76 @@ + (JSTokenButton *)tokenWithString:(NSString *)string representedObject:(id)obj
frame.size.height = 25;
[button setFrame:frame];

[button setToggled:NO];

[button setValue:string];
[button setRepresentedObject:obj];

return button;
}

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

+ (JSTokenButton *)tokenWithView:(UIView<JSTokenButtonCustomView> *)view representedObject:(id)obj {
JSTokenButton *button = (JSTokenButton *)[self buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.f, 0.f, view.frame.size.width, view.frame.size.height);
button.customView = view;
[button addSubview:view];
[button setValue:view];
[button setRepresentedObject:obj];

if (_toggled)
{
[self setBackgroundImage:self.highlightedBg forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
else
{
[self setBackgroundImage:self.normalBg forState:UIControlStateNormal];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
return button;
}

- (void)dealloc
{
self.representedObject = nil;
self.highlightedBg = nil;
self.normalBg = nil;
[super dealloc];

- (void)dealloc {

}


- (BOOL)becomeFirstResponder {
BOOL superReturn = [super becomeFirstResponder];
if (superReturn) {
self.toggled = YES;
self.selected = YES;
if ([_customView respondsToSelector:@selector(buttonStateChanged:)]) {
[_customView buttonStateChanged:self.selected];
}
}
return superReturn;
}


- (BOOL)resignFirstResponder {
BOOL superReturn = [super resignFirstResponder];
if (superReturn) {
self.toggled = NO;
self.selected = NO;
if ([_customView respondsToSelector:@selector(buttonStateChanged:)]) {
[_customView buttonStateChanged:self.selected];
}
}
return superReturn;
}


#pragma mark - UIKeyInput


- (void)deleteBackward {

id <JSTokenFieldDelegate> delegate = _parentField.delegate;
if ([delegate respondsToSelector:@selector(tokenField:shouldRemoveToken:representedObject:)]) {
NSString *name = [self titleForState:UIControlStateNormal];
BOOL shouldRemove = [delegate tokenField:_parentField shouldRemoveToken:name representedObject:self.representedObject];
BOOL shouldRemove = [delegate tokenField:_parentField shouldRemoveToken:self.value representedObject:self.representedObject];
if (!shouldRemove) {
return;
}
}
[_parentField removeTokenForString:[self titleForState:UIControlStateNormal]];

[_parentField removeTokenWithRepresentedObject:self.representedObject];
}


- (BOOL)hasText {
return NO;
}


- (void)insertText:(NSString *)text {
return;
}
Expand Down
29 changes: 13 additions & 16 deletions JSTokenField/JSTokenField.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#import <UIKit/UIKit.h>

@class JSTokenButton;
@protocol JSTokenFieldDelegate;
@protocol JSTokenFieldDelegate, JSTokenButtonCustomView;

extern NSString *const JSTokenFieldFrameDidChangeNotification;
extern NSString *const JSTokenFieldNewFrameKey;
Expand All @@ -38,23 +38,17 @@ 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;
@property (nonatomic) UILabel *label;
@property (nonatomic, readonly) NSMutableArray *tokens;
@property (nonatomic, weak) id <JSTokenFieldDelegate> delegate;
@property (nonatomic) BOOL editing;

- (void)addTokenWithTitle:(NSString *)string representedObject:(id)obj;
- (void)addTokenWithView:(UIView<JSTokenButtonCustomView> *)view representedObject:(id)obj;
- (void)removeTokenForString:(NSString *)string;
- (void)removeTokenWithRepresentedObject:(id)representedObject;
- (void)removeAllTokens;
Expand All @@ -65,14 +59,17 @@ extern NSString *const JSDeletedTokenKey;

@optional

- (void)tokenField:(JSTokenField *)tokenField didAddToken:(NSString *)title representedObject:(id)obj;
- (void)tokenField:(JSTokenField *)tokenField didRemoveToken:(NSString *)title representedObject:(id)obj;
- (BOOL)tokenField:(JSTokenField *)tokenField shouldRemoveToken:(NSString *)title representedObject:(id)obj;

- (void)tokenField:(JSTokenField *)tokenField didAddToken:(id)value representedObject:(id)obj;
- (void)tokenField:(JSTokenField *)tokenField didRemoveToken:(id)value representedObject:(id)obj;
- (BOOL)tokenField:(JSTokenField *)tokenField shouldRemoveToken:(id)value representedObject:(id)obj;

- (void)tokenFieldTextDidChange:(JSTokenField *)tokenField;

- (BOOL)tokenFieldShouldReturn:(JSTokenField *)tokenField;
- (void)tokenFieldDidEndEditing:(JSTokenField *)tokenField;
- (void)tokenFieldDidBeginEditing:(JSTokenField *)tokenField;

- (void)didSelectTokenButton:(JSTokenButton *)tokenButton;
- (void)didUnselectTokenButton:(JSTokenButton *)tokenButton;

@end
Loading