From 233642a12d8ca8435792d0815cf5b3c452a0338c Mon Sep 17 00:00:00 2001 From: Jonathan Crooke Date: Thu, 4 Jun 2015 16:10:01 +0200 Subject: [PATCH] Add support for attributed placeholder --- class/HPGrowingTextView.h | 1 + class/HPGrowingTextView.m | 11 +++++++++++ class/HPTextViewInternal.h | 1 + class/HPTextViewInternal.m | 22 ++++++++++++++++------ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/class/HPGrowingTextView.h b/class/HPGrowingTextView.h index 8b343c5..f3e191a 100644 --- a/class/HPGrowingTextView.h +++ b/class/HPGrowingTextView.h @@ -89,6 +89,7 @@ @property NSTimeInterval animationDuration; @property (nonatomic, strong) NSString *placeholder; @property (nonatomic, strong) UIColor *placeholderColor; +@property (nonatomic, strong) NSAttributedString *attributedPlaceholder; @property (nonatomic, strong) UITextView *internalTextView; diff --git a/class/HPGrowingTextView.m b/class/HPGrowingTextView.m index 9871c6f..ec21bdf 100644 --- a/class/HPGrowingTextView.m +++ b/class/HPGrowingTextView.m @@ -238,6 +238,7 @@ - (NSString *)placeholder - (void)setPlaceholder:(NSString *)placeholder { [internalTextView setPlaceholder:placeholder]; + [internalTextView setAttributedPlaceholder:nil]; [internalTextView setNeedsDisplay]; } @@ -251,6 +252,16 @@ - (void)setPlaceholderColor:(UIColor *)placeholderColor [internalTextView setPlaceholderColor:placeholderColor]; } +- (NSAttributedString *)attributedPlaceholder { + return [internalTextView attributedPlaceholder]; +} + +- (void)setAttributedPlaceholder:(NSAttributedString *)attributedPlaceholder { + [internalTextView setAttributedPlaceholder:attributedPlaceholder]; + [internalTextView setPlaceholder:nil]; + [internalTextView setNeedsDisplay]; +} + - (void)textViewDidChange:(UITextView *)textView { [self refreshHeight]; diff --git a/class/HPTextViewInternal.h b/class/HPTextViewInternal.h index 175f4d4..7267191 100644 --- a/class/HPTextViewInternal.h +++ b/class/HPTextViewInternal.h @@ -32,6 +32,7 @@ @property (nonatomic, strong) NSString *placeholder; @property (nonatomic, strong) UIColor *placeholderColor; +@property (nonatomic, strong) NSAttributedString *attributedPlaceholder; @property (nonatomic) BOOL displayPlaceHolder; @end diff --git a/class/HPTextViewInternal.m b/class/HPTextViewInternal.m index f3a6a1d..75e8e6c 100644 --- a/class/HPTextViewInternal.m +++ b/class/HPTextViewInternal.m @@ -101,17 +101,27 @@ -(void)setContentSize:(CGSize)contentSize - (void)drawRect:(CGRect)rect { [super drawRect:rect]; - if (self.displayPlaceHolder && self.placeholder && self.placeholderColor) + if (self.displayPlaceHolder && (self.attributedPlaceholder || (self.placeholder && self.placeholderColor))) { if ([self respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)]) { - NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; - paragraphStyle.alignment = self.textAlignment; - [self.placeholder drawInRect:CGRectMake(5, 8 + self.contentInset.top, self.frame.size.width-self.contentInset.left, self.frame.size.height- self.contentInset.top) withAttributes:@{NSFontAttributeName:self.font, NSForegroundColorAttributeName:self.placeholderColor, NSParagraphStyleAttributeName:paragraphStyle}]; + CGRect placeholderRect = CGRectMake(5, 8 + self.contentInset.top, self.frame.size.width-self.contentInset.left, self.frame.size.height- self.contentInset.top); + if (self.attributedPlaceholder) { + [self.attributedPlaceholder drawInRect:placeholderRect]; + } else { + NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; + paragraphStyle.alignment = self.textAlignment; + [self.placeholder drawInRect:placeholderRect withAttributes:@{NSFontAttributeName:self.font, NSForegroundColorAttributeName:self.placeholderColor, NSParagraphStyleAttributeName:paragraphStyle}]; + } } else { - [self.placeholderColor set]; - [self.placeholder drawInRect:CGRectMake(8.0f, 8.0f, self.frame.size.width - 16.0f, self.frame.size.height - 16.0f) withFont:self.font]; + CGRect placeholderRect = CGRectMake(8.0f, 8.0f, self.frame.size.width - 16.0f, self.frame.size.height - 16.0f); + if (self.attributedPlaceholder) { + [self.attributedPlaceholder drawInRect:placeholderRect]; + } else { + [self.placeholderColor set]; + [self.placeholder drawInRect:placeholderRect withFont:self.font]; + } } } }