Skip to content

Commit 052e458

Browse files
committed
Improved attributedPlaceholderString appearance on floating label
1 parent 01b177c commit 052e458

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

Example/SMFloatingLabelTextField/SMViewController.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ - (void)viewDidLoad {
2020
[super viewDidLoad];
2121

2222
NSDictionary *lastNamePlaceholderAttributes = @{NSForegroundColorAttributeName: [UIColor magentaColor],
23-
NSFontAttributeName: [UIFont systemFontOfSize:14.0f weight:UIFontWeightBold]
23+
NSFontAttributeName: [UIFont systemFontOfSize:14.0f weight:UIFontWeightBold],
24+
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
2425
};
2526
NSAttributedString *lastNamePlaceholder = [[NSAttributedString alloc] initWithString:@"Last name" attributes:lastNamePlaceholderAttributes];
2627

SMFloatingLabelTextField.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'SMFloatingLabelTextField'
11-
s.version = '0.3.0'
11+
s.version = '0.3.1'
1212
s.summary = 'A subclass of UITextField that displays floating placeholder'
1313

1414
# This description is used to generate tags and improve search results.

SMFloatingLabelTextField/Classes/SMFloatingLabelTextField.m

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,23 @@ - (void)setPlaceholder:(NSString *)placeholder {
174174

175175
- (void)setAttributedPlaceholder:(NSAttributedString *)attributedPlaceholder {
176176
[super setAttributedPlaceholder:attributedPlaceholder];
177-
self.floatingLabel.attributedText = attributedPlaceholder;
177+
self.floatingLabel.attributedText = [self floatingLabelAttributedPlacecholderStringFrom:attributedPlaceholder];
178+
}
179+
180+
- (NSAttributedString *)floatingLabelAttributedPlacecholderStringFrom:(NSAttributedString *)attributedString {
181+
NSMutableAttributedString *mutableAttributedString = [attributedString mutableCopy];
182+
NSRange wholeStringRange = NSMakeRange(0, mutableAttributedString.length);
183+
[mutableAttributedString removeAttribute:NSFontAttributeName range:wholeStringRange];
184+
[mutableAttributedString removeAttribute:NSForegroundColorAttributeName range:wholeStringRange];
185+
186+
[mutableAttributedString addAttribute:NSFontAttributeName value:self.floatingLabelFont range:wholeStringRange];
187+
if (self.displayFloatingPlaceholder) {
188+
[mutableAttributedString addAttribute:NSForegroundColorAttributeName value:self.floatingLabelActiveColor range:wholeStringRange];
189+
} else {
190+
[mutableAttributedString addAttribute:NSForegroundColorAttributeName value:self.floatingLabelPassiveColor range:wholeStringRange];
191+
}
192+
193+
return [mutableAttributedString copy];
178194
}
179195

180196
#pragma mark - dealloc

0 commit comments

Comments
 (0)