From 727a2f0dcdf37b039f11ef3a8019e399057ea9a5 Mon Sep 17 00:00:00 2001 From: Chris Wagner Date: Wed, 4 Dec 2013 14:10:42 -0700 Subject: [PATCH] Add placeholderTextAttributes property to allow for decorating attributedPlaceholder via UIAppearance --- SAMTextView/SAMTextView.h | 9 ++++++++- SAMTextView/SAMTextView.m | 13 ++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/SAMTextView/SAMTextView.h b/SAMTextView/SAMTextView.h index 8da495d..afbb407 100644 --- a/SAMTextView/SAMTextView.h +++ b/SAMTextView/SAMTextView.h @@ -29,8 +29,15 @@ @property (nonatomic, strong) NSAttributedString *attributedPlaceholder; /** - Returns the drawing rectangle for the text views’s placeholder text. + The attributes for the string that is displayed when there is no other text in the text view. + + The default value is `nil`. + */ +@property (nonatomic, strong) NSDictionary *placeholderTextAttributes UI_APPEARANCE_SELECTOR; +/** + Returns the drawing rectangle for the text views’s placeholder text. + @param bounds The bounding rectangle of the receiver. @return The computed drawing rectangle for the placeholder text. */ diff --git a/SAMTextView/SAMTextView.m b/SAMTextView/SAMTextView.m index f520809..791762c 100644 --- a/SAMTextView/SAMTextView.m +++ b/SAMTextView/SAMTextView.m @@ -38,7 +38,9 @@ - (void)setPlaceholder:(NSString *)string { } NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init]; - if (self.typingAttributes) { + if (self.placeholderTextAttributes) { + [attributes addEntriesFromDictionary:self.placeholderTextAttributes]; + } else if (self.typingAttributes) { [attributes addEntriesFromDictionary:self.typingAttributes]; } else { attributes[NSFontAttributeName] = self.font; @@ -70,6 +72,15 @@ - (void)setAttributedPlaceholder:(NSAttributedString *)attributedPlaceholder { [self setNeedsDisplay]; } +- (void)setPlaceholderTextAttributes:(NSDictionary *)placeholderTextAttributes { + if ([placeholderTextAttributes isEqualToDictionary:_placeholderTextAttributes]) { + return; + } + + _placeholderTextAttributes = placeholderTextAttributes; + + self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.attributedPlaceholder.string attributes:_placeholderTextAttributes]; +} - (void)setContentInset:(UIEdgeInsets)contentInset { [super setContentInset:contentInset];