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

Improvements #55

Open
wants to merge 1 commit 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
9 changes: 8 additions & 1 deletion CLTokenInputView/CLTokenInputView/CLTokenInputView.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ NS_ASSUME_NONNULL_BEGIN
@property (copy, nonatomic, nullable) IBInspectable NSString *fieldName;
/** Color of optional */
@property (strong, nonatomic, nullable) IBInspectable UIColor *fieldColor;
@property (strong, nonatomic, nullable) UIFont *font;
@property (strong, nonatomic, nullable) IBInspectable UIColor *commaColor;
@property (copy, nonatomic, nullable) IBInspectable NSString *placeholderText;
@property (strong, nonatomic, nullable) UIView *accessoryView;
@property (assign, nonatomic) IBInspectable UIKeyboardType keyboardType;
@property (assign, nonatomic) IBInspectable UITextAutocapitalizationType autocapitalizationType;
@property (assign, nonatomic) IBInspectable UITextAutocorrectionType autocorrectionType;
@property (assign, nonatomic) IBInspectable UIKeyboardAppearance keyboardAppearance;
@property (strong, nonatomic, nullable) UIView *inputAccessoryView;
/**
* Optional additional characters to trigger the tokenization process (and call the delegate
* with `tokenInputView:tokenForText:`
Expand All @@ -95,11 +98,15 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (copy, nonatomic) CL_GENERIC_SET(NSString *) *tokenizationCharacters;
@property (assign, nonatomic) IBInspectable BOOL drawBottomBorder;
@property (assign, nonatomic) IBInspectable CGFloat verticalSpace;
@property (assign, nonatomic) IBInspectable CGFloat standartRowHeight;
@property (assign, nonatomic) IBInspectable CGFloat minimumHeight;
@property (assign, nonatomic) UIEdgeInsets padding;

@property (readonly, nonatomic) CL_GENERIC_ARRAY(CLToken *) *allTokens;
@property (readonly, nonatomic, getter = isEditing) BOOL editing;
@property (readonly, nonatomic) CGFloat textFieldDisplayOffset;
@property (copy, nonatomic, nullable) NSString *text;
@property (nonatomic, nullable) NSString *text;

- (void)addToken:(CLToken *)token;
- (void)removeToken:(CLToken *)token;
Expand Down
75 changes: 43 additions & 32 deletions CLTokenInputView/CLTokenInputView/CLTokenInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@

static CGFloat const HSPACE = 0.0;
static CGFloat const TEXT_FIELD_HSPACE = 4.0; // Note: Same as CLTokenView.PADDING_X
static CGFloat const VSPACE = 4.0;
static CGFloat const MINIMUM_TEXTFIELD_WIDTH = 56.0;
static CGFloat const PADDING_TOP = 10.0;
static CGFloat const PADDING_BOTTOM = 10.0;
static CGFloat const PADDING_LEFT = 8.0;
static CGFloat const PADDING_RIGHT = 16.0;
static CGFloat const STANDARD_ROW_HEIGHT = 25.0;

static CGFloat const FIELD_MARGIN_X = 4.0; // Note: Same as CLTokenView.PADDING_X

@interface CLTokenInputView () <CLBackspaceDetectingTextFieldDelegate, CLTokenViewDelegate>
Expand All @@ -40,6 +33,11 @@ @implementation CLTokenInputView

- (void)commonInit
{
self.verticalSpace = 4;
self.standartRowHeight = 25;
self.minimumHeight = 45;
self.padding = UIEdgeInsetsMake(10, 8, 10, 16);

self.textField = [[CLBackspaceDetectingTextField alloc] initWithFrame:self.bounds];
self.textField.backgroundColor = [UIColor clearColor];
self.textField.keyboardType = UIKeyboardTypeEmailAddress;
Expand All @@ -66,7 +64,7 @@ - (void)commonInit
[self addSubview:self.fieldLabel];
self.fieldLabel.hidden = YES;

self.intrinsicContentHeight = STANDARD_ROW_HEIGHT;
self.intrinsicContentHeight = self.standartRowHeight;
[self repositionViews];
}

Expand All @@ -90,7 +88,7 @@ - (id)initWithCoder:(NSCoder *)aDecoder

- (CGSize)intrinsicContentSize
{
return CGSizeMake(UIViewNoIntrinsicMetric, MAX(45, self.intrinsicContentHeight));
return CGSizeMake(UIViewNoIntrinsicMetric, MAX(self.minimumHeight, self.intrinsicContentHeight));
}


Expand All @@ -115,6 +113,7 @@ - (void)addToken:(CLToken *)token

[self.tokens addObject:token];
CLTokenView *tokenView = [[CLTokenView alloc] initWithToken:token font:self.textField.font];
tokenView.commaColor = self.commaColor;
if ([self respondsToSelector:@selector(tintColor)]) {
tokenView.tintColor = self.tintColor;
}
Expand Down Expand Up @@ -188,19 +187,19 @@ - (CLToken *)tokenizeTextfieldText
- (void)repositionViews
{
CGRect bounds = self.bounds;
CGFloat rightBoundary = CGRectGetWidth(bounds) - PADDING_RIGHT;
CGFloat rightBoundary = CGRectGetWidth(bounds) - self.padding.right;
CGFloat firstLineRightBoundary = rightBoundary;

CGFloat curX = PADDING_LEFT;
CGFloat curY = PADDING_TOP;
CGFloat totalHeight = STANDARD_ROW_HEIGHT;
CGFloat curX = self.padding.left;
CGFloat curY = self.padding.top;
CGFloat totalHeight = self.standartRowHeight;
BOOL isOnFirstLine = YES;

// Position field view (if set)
if (self.fieldView) {
CGRect fieldViewRect = self.fieldView.frame;
fieldViewRect.origin.x = curX + FIELD_MARGIN_X;
fieldViewRect.origin.y = curY + ((STANDARD_ROW_HEIGHT - CGRectGetHeight(fieldViewRect))/2.0);
fieldViewRect.origin.y = curY + ((self.standartRowHeight - CGRectGetHeight(fieldViewRect))/2.0);
self.fieldView.frame = fieldViewRect;

curX = CGRectGetMaxX(fieldViewRect) + FIELD_MARGIN_X;
Expand All @@ -212,7 +211,7 @@ - (void)repositionViews
CGRect fieldLabelRect = CGRectZero;
fieldLabelRect.size = labelSize;
fieldLabelRect.origin.x = curX + FIELD_MARGIN_X;
fieldLabelRect.origin.y = curY + ((STANDARD_ROW_HEIGHT-CGRectGetHeight(fieldLabelRect))/2.0);
fieldLabelRect.origin.y = curY + ((self.standartRowHeight-CGRectGetHeight(fieldLabelRect))/2.0);
self.fieldLabel.frame = fieldLabelRect;

curX = CGRectGetMaxX(fieldLabelRect) + FIELD_MARGIN_X;
Expand All @@ -221,7 +220,7 @@ - (void)repositionViews
// Position accessory view (if set)
if (self.accessoryView) {
CGRect accessoryRect = self.accessoryView.frame;
accessoryRect.origin.x = CGRectGetWidth(bounds) - PADDING_RIGHT - CGRectGetWidth(accessoryRect);
accessoryRect.origin.x = CGRectGetWidth(bounds) - self.padding.right - CGRectGetWidth(accessoryRect);
accessoryRect.origin.y = curY;
self.accessoryView.frame = accessoryRect;

Expand All @@ -236,15 +235,15 @@ - (void)repositionViews
CGFloat tokenBoundary = isOnFirstLine ? firstLineRightBoundary : rightBoundary;
if (curX + CGRectGetWidth(tokenRect) > tokenBoundary) {
// Need a new line
curX = PADDING_LEFT;
curY += STANDARD_ROW_HEIGHT+VSPACE;
totalHeight += STANDARD_ROW_HEIGHT;
curX = self.padding.left;
curY += self.standartRowHeight+self.verticalSpace;
totalHeight += self.standartRowHeight;
isOnFirstLine = NO;
}

tokenRect.origin.x = curX;
// Center our tokenView vertically within STANDARD_ROW_HEIGHT
tokenRect.origin.y = curY + ((STANDARD_ROW_HEIGHT-CGRectGetHeight(tokenRect))/2.0);
// Center our tokenView vertially within self.standartRowHeight
tokenRect.origin.y = curY + ((self.standartRowHeight-CGRectGetHeight(tokenRect))/2.0);
tokenView.frame = tokenRect;

curX = CGRectGetMaxX(tokenRect) + HSPACE;
Expand All @@ -260,9 +259,9 @@ - (void)repositionViews
// isOnFirstLine will be useful, and this calculation is important.
// So leaving it set here, and marking the warning to ignore it
#pragma unused(isOnFirstLine)
curX = PADDING_LEFT + TEXT_FIELD_HSPACE;
curY += STANDARD_ROW_HEIGHT+VSPACE;
totalHeight += STANDARD_ROW_HEIGHT;
curX = self.padding.left + TEXT_FIELD_HSPACE;
curY += self.standartRowHeight+self.verticalSpace;
totalHeight += self.standartRowHeight;
// Adjust the width
availableWidthForTextField = rightBoundary - curX;
}
Expand All @@ -271,11 +270,11 @@ - (void)repositionViews
textFieldRect.origin.x = curX;
textFieldRect.origin.y = curY + self.additionalTextFieldYOffset;
textFieldRect.size.width = availableWidthForTextField;
textFieldRect.size.height = STANDARD_ROW_HEIGHT;
textFieldRect.size.height = self.standartRowHeight;
self.textField.frame = textFieldRect;

CGFloat oldContentHeight = self.intrinsicContentHeight;
self.intrinsicContentHeight = MAX(totalHeight, CGRectGetMaxY(textFieldRect)+PADDING_BOTTOM);
self.intrinsicContentHeight = MAX(totalHeight, CGRectGetMaxY(textFieldRect)+self.padding.bottom);
[self invalidateIntrinsicContentSize];

if (oldContentHeight != self.intrinsicContentHeight) {
Expand Down Expand Up @@ -401,28 +400,33 @@ - (void)setKeyboardAppearance:(UIKeyboardAppearance)keyboardAppearance
}


- (void)setFont:(UIFont *)font {
_font = font;
self.textField.font = font;
}


#pragma mark - Measurements (text field offset, etc.)

- (CGFloat)textFieldDisplayOffset
{
// Essentially the textfield's y with PADDING_TOP
return CGRectGetMinY(self.textField.frame) - PADDING_TOP;
// Essentially the textfield's y with self.padding.top
return CGRectGetMinY(self.textField.frame) - self.padding.top;
}


#pragma mark - Textfield text

- (void)setText:(NSString * _Nullable)text {
self.textField.text = text;
}

- (NSString *)text
{
return self.textField.text;
}


-(void) setText:(NSString*)text {
self.textField.text = text;
}

#pragma mark - CLTokenViewDelegate

- (void)tokenViewDidRequestDelete:(CLTokenView *)tokenView replaceWithText:(NSString *)replacementText
Expand Down Expand Up @@ -558,6 +562,13 @@ - (void)setAccessoryView:(UIView *)accessoryView
[self repositionViews];
}

- (void)setInputAccessoryView:(UIView *)inputAccessoryView {
_textField.inputAccessoryView = inputAccessoryView;
}

- (UIView *)inputAccessoryView {
return _textField.inputAccessoryView;
}

#pragma mark - Drawing

Expand Down
1 change: 1 addition & 0 deletions CLTokenInputView/CLTokenInputView/CLTokenView.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (weak, nonatomic, nullable) NSObject <CLTokenViewDelegate> *delegate;
@property (assign, nonatomic) BOOL selected;
@property (assign, nonatomic) BOOL hideUnselectedComma;
@property (strong, nonatomic) UIColor *commaColor;

- (id)initWithToken:(CLToken *)token font:(nullable UIFont *)font;

Expand Down
4 changes: 3 additions & 1 deletion CLTokenInputView/CLTokenInputView/CLTokenView.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ - (id)initWithToken:(CLToken *)token font:(nullable UIFont *)font
{
self = [super initWithFrame:CGRectZero];
if (self) {
self.commaColor = [UIColor lightGrayColor];

UIColor *tintColor = [UIColor colorWithRed:0.0823 green:0.4941 blue:0.9843 alpha:1.0];
if ([self respondsToSelector:@selector(tintColor)]) {
tintColor = self.tintColor;
Expand Down Expand Up @@ -185,7 +187,7 @@ - (void)updateLabelAttributedText
NSMutableAttributedString *attrString =
[[NSMutableAttributedString alloc] initWithString:labelString
attributes:@{NSFontAttributeName : self.label.font,
NSForegroundColorAttributeName : [UIColor lightGrayColor]}];
NSForegroundColorAttributeName : self.commaColor}];
NSRange tintRange = [labelString rangeOfString:self.displayText];
// Make the name part the system tint color
UIColor *tintColor = self.selectedBackgroundView.backgroundColor;
Expand Down