Skip to content

Commit

Permalink
Add support for invalidity
Browse files Browse the repository at this point in the history
  • Loading branch information
gravicle committed Mar 25, 2016
1 parent 429f50d commit 8e6c1e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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];
}
Expand Down

0 comments on commit 8e6c1e0

Please sign in to comment.