Skip to content
This repository has been archived by the owner on Apr 22, 2019. It is now read-only.

Add support for NSAttributedString placeholder #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions class/HPGrowingTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down
11 changes: 11 additions & 0 deletions class/HPGrowingTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ - (NSString *)placeholder
- (void)setPlaceholder:(NSString *)placeholder
{
[internalTextView setPlaceholder:placeholder];
[internalTextView setAttributedPlaceholder:nil];
[internalTextView setNeedsDisplay];
}

Expand All @@ -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];
Expand Down
1 change: 1 addition & 0 deletions class/HPTextViewInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

@property (nonatomic, strong) NSString *placeholder;
@property (nonatomic, strong) UIColor *placeholderColor;
@property (nonatomic, strong) NSAttributedString *attributedPlaceholder;
@property (nonatomic) BOOL displayPlaceHolder;

@end
22 changes: 16 additions & 6 deletions class/HPTextViewInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
}
}
Expand Down