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
1 change: 1 addition & 0 deletions VENTokenField/VENToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
@property (strong, nonatomic) UIColor *colorScheme;

- (void)setTitleText:(NSString *)text;
- (void)setAttributedTitleText:(NSAttributedString *)attributedTitleText;

@end
11 changes: 11 additions & 0 deletions VENTokenField/VENToken.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ - (void)setUpInit
- (void)setTitleText:(NSString *)text
{
self.titleLabel.text = text;
[self setText];
}

- (void)setAttributedTitleText:(NSAttributedString *)attributedTitleText
{
self.titleLabel.attributedText = attributedTitleText;
[self setText];
}

- (void)setText
{
self.titleLabel.textColor = self.colorScheme;
[self.titleLabel sizeToFit];
self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame), CGRectGetMaxX(self.titleLabel.frame) + 3, CGRectGetHeight(self.frame));
Expand Down
1 change: 1 addition & 0 deletions VENTokenField/VENTokenField.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
@protocol VENTokenFieldDataSource <NSObject>
@optional
- (NSString *)tokenField:(VENTokenField *)tokenField titleForTokenAtIndex:(NSUInteger)index;
- (NSAttributedString *)tokenField:(VENTokenField *)tokenField attributedTitleForTokenAtIndex:(NSUInteger)index;
- (NSUInteger)numberOfTokensInTokenField:(VENTokenField *)tokenField;
- (NSString *)tokenFieldCollapsedText:(VENTokenField *)tokenField;
@end
Expand Down
30 changes: 27 additions & 3 deletions VENTokenField/VENTokenField.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ @interface VENTokenField () <VENBackspaceTextFieldDelegate>
@property (strong, nonatomic) VENBackspaceTextField *inputTextField;
@property (strong, nonatomic) UIColor *colorScheme;
@property (strong, nonatomic) UILabel *collapsedLabel;
@property (nonatomic, readonly) BOOL isAttributedToken;

@end

Expand Down Expand Up @@ -248,16 +249,26 @@ - (void)layoutToLabelInView:(UIView *)view origin:(CGPoint)origin currentX:(CGFl
- (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)currentY
{
for (NSUInteger i = 0; i < [self numberOfTokens]; i++) {
NSString *title = [self titleForTokenAtIndex:i];
VENToken *token = [[VENToken alloc] init];
token.colorScheme = self.colorScheme;

__weak VENToken *weakToken = token;
token.didTapTokenBlock = ^{
[self didTapToken:weakToken];
};

[token setTitleText:[NSString stringWithFormat:@"%@,", title]];

if ([self isAttributedToken]) {
NSAttributedString *attributedString = [self attributedTitleForTokenAtIndex:i];
NSRange range = NSMakeRange(0, attributedString.length);
attributedString = [[NSAttributedString alloc]
initWithString:[NSString stringWithFormat:@"%@,", attributedString.string]
attributes:[attributedString attributesAtIndex:0 effectiveRange:&range]];

[token setAttributedTitleText:attributedString];
} else {
[token setTitleText:[NSString stringWithFormat:@"%@,",[self titleForTokenAtIndex:i]]];
}

[self.tokens addObject:token];

if (*currentX + token.width <= self.scrollView.contentSize.width) { // token fits in current line
Expand All @@ -284,6 +295,11 @@ - (CGFloat)heightForToken
return 30;
}

- (BOOL)isAttributedToken
{
return [self.dataSource respondsToSelector:@selector(tokenField:attributedTitleForTokenAtIndex:)];
}

- (void)layoutInvisibleTextField
{
self.invisibleTextField = [[VENBackspaceTextField alloc] initWithFrame:CGRectZero];
Expand Down Expand Up @@ -429,6 +445,14 @@ - (NSString *)titleForTokenAtIndex:(NSUInteger)index
return [NSString string];
}

- (NSAttributedString *)attributedTitleForTokenAtIndex:(NSUInteger)index
{
if([self isAttributedToken]) {
return [self.dataSource tokenField:self attributedTitleForTokenAtIndex:index];
}
return [NSAttributedString new];
}

- (NSUInteger)numberOfTokens
{
if ([self.dataSource respondsToSelector:@selector(numberOfTokensInTokenField:)]) {
Expand Down