Created
October 27, 2014 11:25
-
-
Save nicpro85/5b67efd3617caaef9e44 to your computer and use it in GitHub Desktop.
UIPlaceholderTextView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIPlaceholderTextView.h | |
// | |
// Created by Nicolas Manzini on 27.10.14. | |
// Free under MIT | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIPlaceholderTextView : UITextView | |
@property (strong, nonatomic) NSAttributedString * attributedPlaceholder; | |
@property (assign, nonatomic) CGPoint placeholderOffset; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIPlaceholderTextView.m | |
// | |
// Created by Nicolas Manzini on 27.10.14. | |
// Free under MIT | |
// | |
#import "UIPlaceholderTextView.h" | |
const CGPoint defaultPlaceholderOffset = {5.f,7.f}; | |
@interface UIPlaceholderTextView () | |
@property (strong, nonatomic) UILabel * labelPlaceholder; | |
- (void)textDidChange:(NSNotification *)note; | |
- (void)initLabelPlaceholder; | |
@end | |
@implementation UIPlaceholderTextView | |
- (id)initWithCoder:(NSCoder *)aDecoder { | |
self = [super initWithCoder:aDecoder]; | |
if (self) { | |
_placeholderOffset = defaultPlaceholderOffset; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:nil]; | |
[self initLabelPlaceholder]; | |
} | |
return self; | |
} | |
- (instancetype)init { | |
self = [super init]; | |
if (self) { | |
_placeholderOffset = defaultPlaceholderOffset; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:nil]; | |
[self initLabelPlaceholder]; | |
} | |
return self; | |
} | |
- (instancetype)initWithFrame:(CGRect)frame { | |
self = [super initWithFrame:frame]; | |
if (self) { | |
_placeholderOffset = defaultPlaceholderOffset; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:nil]; | |
[self initLabelPlaceholder]; | |
} | |
return self; | |
} | |
- (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer { | |
self = [super initWithFrame:frame textContainer:textContainer]; | |
if (self) { | |
_placeholderOffset = defaultPlaceholderOffset; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:nil]; | |
[self initLabelPlaceholder]; | |
} | |
return self; | |
} | |
- (void)initLabelPlaceholder { | |
UIEdgeInsets insets = self.contentInset; | |
CGPoint offset = self.contentOffset; | |
CGPoint pOffset = self.placeholderOffset; | |
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(insets.left+offset.x+pOffset.x, insets.top+ offset.y+pOffset.y, 0.f, 0.f)]; | |
label.backgroundColor = [UIColor clearColor]; | |
label.userInteractionEnabled = NO; | |
[self addSubview:label]; | |
self.labelPlaceholder = label; | |
} | |
- (void)setPlaceholderOffset:(CGPoint)placeholderOffset { | |
CGPoint previousOffset = _placeholderOffset; | |
_placeholderOffset = placeholderOffset; | |
CGRect frame = self.labelPlaceholder.frame; | |
frame.origin.x -= previousOffset.x; | |
frame.origin.x += placeholderOffset.x; | |
frame.origin.y -= previousOffset.y; | |
frame.origin.x += placeholderOffset.y; | |
self.labelPlaceholder.frame = frame; | |
} | |
- (void)textDidChange:(NSNotification *)note { | |
if (note.object == self) { | |
self.labelPlaceholder.hidden = self.text.length; | |
} | |
} | |
- (void)setAttributedPlaceholder:(NSAttributedString *)attributedPlaceholder { | |
_attributedPlaceholder = attributedPlaceholder; | |
self.labelPlaceholder.attributedText = attributedPlaceholder; | |
if (attributedPlaceholder) { | |
[self resizeLabelPlaceholder]; | |
} | |
} | |
- (void)resizeLabelPlaceholder { | |
NSStringDrawingContext * context = [NSStringDrawingContext new]; | |
context.minimumScaleFactor = 0.f; | |
CGRect bounds = | |
[self.labelPlaceholder.attributedText boundingRectWithSize:self.bounds.size | |
options:NSStringDrawingUsesLineFragmentOrigin | |
context:context]; | |
CGRect frame = self.labelPlaceholder.frame; | |
frame.size.height = bounds.size.height; | |
frame.size.width = bounds.size.width; | |
self.labelPlaceholder.frame = frame; | |
} | |
- (void)setFrame:(CGRect)frame { | |
[super setFrame:frame]; | |
[self resizeLabelPlaceholder]; | |
} | |
- (void)setBounds:(CGRect)bounds { | |
[super setBounds:bounds]; | |
[self resizeLabelPlaceholder]; | |
} | |
- (void)dealloc { | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment