From 394fab20a4967f0b9487d029e59a6d03949183d2 Mon Sep 17 00:00:00 2001 From: Viacheslav Karamov Date: Fri, 9 Aug 2019 11:39:13 +0300 Subject: [PATCH] Add ability to display placeholder on top --- MaterialTextField/MFTextField.h | 6 ++++++ MaterialTextField/MFTextField.m | 12 +++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) mode change 100644 => 100755 MaterialTextField/MFTextField.h mode change 100644 => 100755 MaterialTextField/MFTextField.m diff --git a/MaterialTextField/MFTextField.h b/MaterialTextField/MFTextField.h old mode 100644 new mode 100755 index 210e2bd..856c748 --- a/MaterialTextField/MFTextField.h +++ b/MaterialTextField/MFTextField.h @@ -28,6 +28,12 @@ IB_DESIGNABLE */ @property (nonatomic) IBInspectable BOOL placeholderAnimatesOnFocus; + +/** + * Default is NO. If YES, the placeholder label will be always visible + */ +@property (nonatomic) IBInspectable BOOL alwaysShowPlaceholderOnTop; + /** * The color of the text field's default placeholder (displayed when text field is empty). */ diff --git a/MaterialTextField/MFTextField.m b/MaterialTextField/MFTextField.m old mode 100644 new mode 100755 index a45b10d..cfb680b --- a/MaterialTextField/MFTextField.m +++ b/MaterialTextField/MFTextField.m @@ -76,6 +76,7 @@ - (void)setDefaults self.textPadding = CGSizeMake(0.0f, 8.0f); self.errorPadding = CGSizeMake(0.0f, 4.0f); + self.alwaysShowPlaceholderOnTop = NO; self.animatesPlaceholder = YES; self.placeholderColor = [UIColor mf_darkGrayColor]; self.placeholderFont = self.defaultPlaceholderFont; @@ -243,6 +244,15 @@ - (void)setAnimatesPlaceholder:(BOOL)animatesPlaceholder } } +- (void)setAlwaysShowPlaceholderOnTop:(BOOL)alwaysShowPlaceholderOnTop +{ + _alwaysShowPlaceholderOnTop = alwaysShowPlaceholderOnTop; + if (alwaysShowPlaceholderOnTop) + { + self.placeholderLabel.alpha = 1; + } +} + - (void)setDefaultPlaceholderColor:(UIColor *)defaultPlaceholderColor { _defaultPlaceholderColor = defaultPlaceholderColor ?: [UIColor mf_defaultPlaceholderGray]; @@ -299,7 +309,7 @@ - (BOOL)shouldShowPlaceholder { BOOL isEmpty = self.text.length == 0; - return !isEmpty || (self.placeholderAnimatesOnFocus && self.isFirstResponder); + return !isEmpty || self.alwaysShowPlaceholderOnTop || (self.placeholderAnimatesOnFocus && self.isFirstResponder); } - (BOOL)hasError