Created
January 18, 2012 21:21
-
-
Save jnjosh/1635731 to your computer and use it in GitHub Desktop.
TextView with a Placeholder option
This file contains hidden or 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
// | |
// TTPlaceholderTextView.h | |
// | |
// Created by Joshua Johnson on 1/6/12. | |
// Copyright (c) 2012 Two Toasters. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface TTPlaceholderTextView : UITextView | |
@property (nonatomic, retain) UIColor *placeholderColor; | |
@property (nonatomic, copy) NSString *placeholderText; | |
@end |
This file contains hidden or 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
// | |
// TTPlaceholderTextView.m | |
// | |
// Created by Joshua Johnson on 1/6/12. | |
// Copyright (c) 2012 Two Toasters. All rights reserved. | |
// | |
#import "TTPlaceholderTextView.h" | |
@interface TTPlaceholderTextView () | |
@property (nonatomic, assign) BOOL shouldDrawPlaceholder; | |
- (void)contentDidChange:(NSNotification *)notification; | |
- (CGRect)proportionalInsetRectFromRect:(CGRect)rect inset:(CGFloat)inset; | |
@end | |
@implementation TTPlaceholderTextView | |
#pragma mark - synth | |
@synthesize placeholderText = _placeholderText, _placeholderColor = _placeholderColor; | |
@synthesize shouldDrawPlaceholder = _shouldDrawPlaceholder; | |
#pragma mark - lifecycle | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
if (self = [super initWithFrame:frame]) { | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentDidChange:) name:UITextViewTextDidChangeNotification object:self]; | |
_placeholderColor = [[UIColor lightGrayColor] retain]; | |
_shouldDrawPlaceholder = YES; | |
[self setNeedsDisplay]; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
[_placeholderText release]; | |
[_placeholderColor release]; | |
[super dealloc]; | |
} | |
#pragma mark - Drawing | |
- (void)drawRect:(CGRect)rect | |
{ | |
[super drawRect:rect]; | |
if (_shouldDrawPlaceholder) { | |
[_placeholderColor set]; | |
[_placeholderText drawInRect:[self proportionalInsetRectFromRect:rect inset:8.0] withFont:[self font]]; | |
} | |
} | |
#pragma mark - helpers | |
- (CGRect)proportionalInsetRectFromRect:(CGRect)rect inset:(CGFloat)inset | |
{ | |
CGFloat insets = inset * 2; | |
return (CGRect){rect.origin.x + inset, rect.origin.y + inset, rect.size.width - insets, rect.size.height - insets}; | |
} | |
#pragma mark - notification | |
- (void)contentDidChange:(NSNotification *)notification | |
{ | |
BOOL previousState = _shouldDrawPlaceholder; | |
_shouldDrawPlaceholder = _placeholderText && _placeholderColor && [[self text] length] == 0; | |
if (previousState != _shouldDrawPlaceholder) { | |
[self setNeedsDisplay]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment