diff --git a/VENTokenField/VENToken.h b/VENTokenField/VENToken.h index 21768cb..b7d18b7 100644 --- a/VENTokenField/VENToken.h +++ b/VENTokenField/VENToken.h @@ -29,5 +29,6 @@ @property (strong, nonatomic) UIColor *colorScheme; - (void)setTitleText:(NSString *)text; +- (void)setAttributedTitleText:(NSAttributedString *)attributedTitleText; @end diff --git a/VENTokenField/VENToken.m b/VENTokenField/VENToken.m index 4435a83..55dd9ac 100644 --- a/VENTokenField/VENToken.m +++ b/VENTokenField/VENToken.m @@ -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)); diff --git a/VENTokenField/VENTokenField.h b/VENTokenField/VENTokenField.h index 1288ef3..893f417 100644 --- a/VENTokenField/VENTokenField.h +++ b/VENTokenField/VENTokenField.h @@ -34,6 +34,7 @@ @protocol VENTokenFieldDataSource @optional - (NSString *)tokenField:(VENTokenField *)tokenField titleForTokenAtIndex:(NSUInteger)index; +- (NSAttributedString *)tokenField:(VENTokenField *)tokenField attributedTitleForTokenAtIndex:(NSUInteger)index; - (NSUInteger)numberOfTokensInTokenField:(VENTokenField *)tokenField; - (NSString *)tokenFieldCollapsedText:(VENTokenField *)tokenField; @end diff --git a/VENTokenField/VENTokenField.m b/VENTokenField/VENTokenField.m index e51040a..7ffa2be 100644 --- a/VENTokenField/VENTokenField.m +++ b/VENTokenField/VENTokenField.m @@ -44,6 +44,7 @@ @interface VENTokenField () @property (strong, nonatomic) VENBackspaceTextField *inputTextField; @property (strong, nonatomic) UIColor *colorScheme; @property (strong, nonatomic) UILabel *collapsedLabel; +@property (nonatomic, readonly) BOOL isAttributedToken; @end @@ -248,7 +249,6 @@ - (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; @@ -256,8 +256,19 @@ - (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)current 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 @@ -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]; @@ -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:)]) {