From 8e6c1e0d45a0e65cec261505d964b09a3b32c7b3 Mon Sep 17 00:00:00 2001 From: Amit Jain Date: Fri, 25 Mar 2016 14:44:21 -0500 Subject: [PATCH] Add support for invalidity --- .../JVFloatLabeledTextField.h | 11 +++++++++++ .../JVFloatLabeledTextField.m | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/JVFloatLabeledTextField/JVFloatLabeledTextField/JVFloatLabeledTextField.h b/JVFloatLabeledTextField/JVFloatLabeledTextField/JVFloatLabeledTextField.h index 00be5ec..afd73ad 100644 --- a/JVFloatLabeledTextField/JVFloatLabeledTextField/JVFloatLabeledTextField.h +++ b/JVFloatLabeledTextField/JVFloatLabeledTextField/JVFloatLabeledTextField.h @@ -48,6 +48,11 @@ IB_DESIGNABLE */ @property (nonatomic, strong, readonly) UILabel * floatingLabel; +/** + * Applied `floatingLabelInvalidTextColor` when true and the field is not a first responder + */ +@property (nonatomic) BOOL hasInvalidInput; + /** * Padding to be applied to the y coordinate of the floating label upon presentation. * Defaults to zero. @@ -86,6 +91,12 @@ IB_DESIGNABLE */ @property (nonatomic, strong) IBInspectable UIColor * floatingLabelActiveTextColor; +/** + * Text color to be applied to the floating label while the field has `invalidInput`. + * The effect is disabled if an `floatingLabelInvalidTextColor` is not provided. + */ +@property (nonatomic, strong) IBInspectable UIColor * floatingLabelInvalidTextColor; + /** * Indicates whether the floating label's appearance should be animated regardless of first responder status. * By default, animation only occurs if the text field is a first responder. diff --git a/JVFloatLabeledTextField/JVFloatLabeledTextField/JVFloatLabeledTextField.m b/JVFloatLabeledTextField/JVFloatLabeledTextField/JVFloatLabeledTextField.m index 1f0f9a6..cd4ce2f 100644 --- a/JVFloatLabeledTextField/JVFloatLabeledTextField/JVFloatLabeledTextField.m +++ b/JVFloatLabeledTextField/JVFloatLabeledTextField/JVFloatLabeledTextField.m @@ -117,6 +117,16 @@ - (UIColor *)labelActiveColor return [UIColor blueColor]; } +- (UIColor *)labelInvalidColor +{ + if (_floatingLabelInvalidTextColor) { + return _floatingLabelInvalidTextColor; + } + else { + return [self labelActiveColor]; + } +} + - (void)setFloatingLabelFont:(UIFont *)floatingLabelFont { if (floatingLabelFont != nil) { @@ -337,8 +347,10 @@ - (void)layoutSubviews floatingLabelSize.height); BOOL firstResponder = self.isFirstResponder; + BOOL hasInvalidInput = self.hasInvalidInput; _floatingLabel.textColor = (firstResponder && self.text && self.text.length > 0 ? self.labelActiveColor : self.floatingLabelTextColor); + if ((!firstResponder && hasInvalidInput)) { _floatingLabel.textColor = self.labelInvalidColor; } if ((!self.text || 0 == [self.text length]) && !self.alwaysShowFloatingLabel) { [self hideFloatingLabel:firstResponder]; }