Skip to content
Browse files

Changed kTextEmpty/Hidden chars. Other minor cleanup.

  • Loading branch information...
1 parent bdf2576 commit 197fa2d71a8b5d79987b7d6e6c5d334ef8cc6d65 @thermogl committed Apr 8, 2012
Showing with 29 additions and 36 deletions.
  1. +4 −5 TITokenField.h
  2. +24 −30 TITokenField.m
  3. +1 −1 TokenFieldExample/Classes/TokenFieldExampleViewController.m
View
9 TITokenField.h
@@ -156,22 +156,21 @@ typedef enum {
@interface TIToken : UIControl {
NSString * title;
+ id representedObject;
UIFont * font;
UIColor * tintColor;
- CGFloat maxWidth;
TITokenAccessoryType accessoryType;
-
- id representedObject;
+ CGFloat maxWidth;
}
@property (nonatomic, copy) NSString * title;
+@property (nonatomic, retain) id representedObject;
@property (nonatomic, retain) UIFont * font;
@property (nonatomic, retain) UIColor * tintColor UI_APPEARANCE_SELECTOR;
-@property (nonatomic, assign) CGFloat maxWidth;
@property (nonatomic, assign) TITokenAccessoryType accessoryType;
-@property (nonatomic, retain) id representedObject;
+@property (nonatomic, assign) CGFloat maxWidth;
- (id)initWithTitle:(NSString *)aTitle;
- (id)initWithTitle:(NSString *)aTitle representedObject:(id)object;
View
54 TITokenField.m
@@ -218,7 +218,7 @@ - (void)tokenFieldDidEndEditing:(TITokenField *)field {
}
- (void)tokenFieldTextDidChange:(TITokenField *)field {
- [self resultsForSubstring:field.text];
+ [self resultsForSubstring:[field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
- (void)tokenFieldFrameWillChange:(TITokenField *)field {
@@ -281,17 +281,14 @@ - (void)resultsForSubstring:(NSString *)substring {
[resultsArray removeAllObjects];
[resultsTable reloadData];
- NSString * strippedString = [[substring stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] lowercaseString];
-
NSArray * sourceCopy = [sourceArray copy];
for (NSString * sourceObject in sourceCopy){
- NSString * query = [[self searchResultStringForRepresentedObject:sourceObject] lowercaseString];
- if ([query rangeOfString:strippedString].location != NSNotFound){
-
- BOOL shouldAdd = YES;
+ NSString * query = [self searchResultStringForRepresentedObject:sourceObject];
+ if ([query rangeOfString:substring options:NSCaseInsensitiveSearch].location != NSNotFound){
- if (!showAlreadyTokenized){
+ BOOL shouldAdd = ![resultsArray containsObject:sourceObject];
+ if (shouldAdd && !showAlreadyTokenized){
for (TIToken * token in tokenField.tokens){
@@ -302,9 +299,7 @@ - (void)resultsForSubstring:(NSString *)substring {
}
}
- if (shouldAdd && ![resultsArray containsObject:sourceObject]){
- [resultsArray addObject:sourceObject];
- }
+ if (shouldAdd) [resultsArray addObject:sourceObject];
}
}
@@ -319,9 +314,8 @@ - (void)resultsForSubstring:(NSString *)substring {
- (void)presentpopoverAtTokenFieldCaretAnimated:(BOOL)animated {
UITextPosition * position = [tokenField positionFromPosition:tokenField.beginningOfDocument offset:2];
- CGRect caretRect = [tokenField caretRectForPosition:position];
- [popoverController presentPopoverFromRect:caretRect inView:tokenField
+ [popoverController presentPopoverFromRect:[tokenField caretRectForPosition:position] inView:tokenField
permittedArrowDirections:UIPopoverArrowDirectionUp animated:animated];
}
@@ -343,8 +337,8 @@ - (void)dealloc {
//==========================================================
#pragma mark - TITokenField -
//==========================================================
-NSString * const kTextEmpty = @" "; // Just a space
-NSString * const kTextHidden = @"`"; // This character isn't available on iOS (yet) so it's safe.
+NSString * const kTextEmpty = @"\u200B"; // Zero-Width Space
+NSString * const kTextHidden = @"\u200D"; // Zero-Width Joiner
@interface TITokenFieldInternalDelegate ()
@property (nonatomic, assign) id <UITextFieldDelegate> delegate;
@@ -741,8 +735,8 @@ - (CGRect)textRectForBounds:(CGRect)bounds {
if ([self.text isEqualToString:kTextHidden]) return CGRectMake(0, -20, 0, 0);
- CGRect frame = CGRectOffset(bounds, tokenCaret.x, tokenCaret.y + 3);
- frame.size.width -= (tokenCaret.x + self.rightViewWidth + 8);
+ CGRect frame = CGRectOffset(bounds, tokenCaret.x + 2, tokenCaret.y + 3);
+ frame.size.width -= (tokenCaret.x + self.rightViewWidth + 10);
return frame;
}
@@ -900,11 +894,11 @@ - (BOOL)getTintColorRed:(CGFloat *)red green:(CGFloat *)green blue:(CGFloat *)bl
@implementation TIToken
@synthesize title;
+@synthesize representedObject;
@synthesize font;
@synthesize tintColor;
-@synthesize maxWidth;
@synthesize accessoryType;
-@synthesize representedObject;
+@synthesize maxWidth;
#pragma mark Init
- (id)initWithTitle:(NSString *)aTitle {
@@ -921,13 +915,13 @@ - (id)initWithTitle:(NSString *)aTitle representedObject:(id)object font:(UIFont
title = [aTitle copy];
representedObject = [object retain];
- accessoryType = TITokenAccessoryTypeNone;
font = [aFont retain];
- maxWidth = 200;
-
tintColor = [[TIToken blueTintColor] retain];
+ accessoryType = TITokenAccessoryTypeNone;
+ maxWidth = 200;
+
[self setBackgroundColor:[UIColor clearColor]];
[self sizeToFit];
}
@@ -985,18 +979,18 @@ - (void)setTintColor:(UIColor *)newTintColor {
}
}
-- (void)setMaxWidth:(CGFloat)width {
+- (void)setAccessoryType:(TITokenAccessoryType)type {
- if (maxWidth != width){
- maxWidth = width;
+ if (accessoryType != type){
+ accessoryType = type;
[self sizeToFit];
}
}
-- (void)setAccessoryType:(TITokenAccessoryType)type {
+- (void)setMaxWidth:(CGFloat)width {
- if (accessoryType != type){
- accessoryType = type;
+ if (maxWidth != width){
+ maxWidth = width;
[self sizeToFit];
}
}
@@ -1210,14 +1204,14 @@ - (BOOL)getTintColorRed:(CGFloat *)red green:(CGFloat *)green blue:(CGFloat *)bl
#pragma mark Other
- (NSString *)description {
- return [NSString stringWithFormat:@"<TIToken %p; title = \"%@\">", self, title];
+ return [NSString stringWithFormat:@"<TIToken %p; title = \"%@\"; representedObject = \"%@\">", self, title, representedObject];
}
- (void)dealloc {
[title release];
+ [representedObject release];
[font release];
[tintColor release];
- [representedObject release];
[super dealloc];
}
View
2 TokenFieldExample/Classes/TokenFieldExampleViewController.m
@@ -18,7 +18,7 @@ @implementation TokenFieldExampleViewController
- (void)viewDidLoad {
[self.view setBackgroundColor:[UIColor whiteColor]];
- [self.navigationItem setTitle:@"TITokenFieldView"];
+ [self.navigationItem setTitle:@"Example"];
tokenFieldView = [[TITokenFieldView alloc] initWithFrame:self.view.bounds];
[tokenFieldView setSourceArray:[Names listOfNames]];

0 comments on commit 197fa2d

Please sign in to comment.
Something went wrong with that request. Please try again.