diff --git a/CLTokenInputView/CLTokenInputView/CLTokenInputView.h b/CLTokenInputView/CLTokenInputView/CLTokenInputView.h index 32ebf8c..f796f6b 100644 --- a/CLTokenInputView/CLTokenInputView/CLTokenInputView.h +++ b/CLTokenInputView/CLTokenInputView/CLTokenInputView.h @@ -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:` @@ -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; diff --git a/CLTokenInputView/CLTokenInputView/CLTokenInputView.m b/CLTokenInputView/CLTokenInputView/CLTokenInputView.m index 5077c14..9f1930a 100644 --- a/CLTokenInputView/CLTokenInputView/CLTokenInputView.m +++ b/CLTokenInputView/CLTokenInputView/CLTokenInputView.m @@ -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 () @@ -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; @@ -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]; } @@ -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)); } @@ -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; } @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; } @@ -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) { @@ -401,17 +400,26 @@ - (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 { @@ -419,10 +427,6 @@ - (NSString *)text } --(void) setText:(NSString*)text { - self.textField.text = text; -} - #pragma mark - CLTokenViewDelegate - (void)tokenViewDidRequestDelete:(CLTokenView *)tokenView replaceWithText:(NSString *)replacementText @@ -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 diff --git a/CLTokenInputView/CLTokenInputView/CLTokenView.h b/CLTokenInputView/CLTokenInputView/CLTokenView.h index 9956718..9b2c9ad 100644 --- a/CLTokenInputView/CLTokenInputView/CLTokenView.h +++ b/CLTokenInputView/CLTokenInputView/CLTokenView.h @@ -27,6 +27,7 @@ NS_ASSUME_NONNULL_BEGIN @property (weak, nonatomic, nullable) NSObject *delegate; @property (assign, nonatomic) BOOL selected; @property (assign, nonatomic) BOOL hideUnselectedComma; +@property (strong, nonatomic) UIColor *commaColor; - (id)initWithToken:(CLToken *)token font:(nullable UIFont *)font; diff --git a/CLTokenInputView/CLTokenInputView/CLTokenView.m b/CLTokenInputView/CLTokenInputView/CLTokenView.m index 2cffcf5..7da1a86 100644 --- a/CLTokenInputView/CLTokenInputView/CLTokenView.m +++ b/CLTokenInputView/CLTokenInputView/CLTokenView.m @@ -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; @@ -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;