Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: ios5
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 97 lines (67 sloc) 2.042 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
//
// SSTextView.m
// SSToolkit
//
// Created by Sam Soffes on 8/18/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//

#import "SSTextView.h"

@interface SSTextView (PrivateMethods)
- (void)_updateShouldDrawPlaceholder;
- (void)_textChanged:(NSNotification *)notification;
@end


@implementation SSTextView

#pragma mark -
#pragma mark Accessors

@synthesize placeholder = _placeholder;
@synthesize placeholderColor = _placeholderColor;

- (void)setText:(NSString *)string {
[super setText:string];
[self _updateShouldDrawPlaceholder];
}


- (void)setPlaceholder:(NSString *)string {
if ([string isEqual:_placeholder]) {
return;
}

[_placeholder release];
_placeholder = [string retain];

[self _updateShouldDrawPlaceholder];
}


#pragma mark -
#pragma mark NSObject

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self];

[_placeholder release];
[_placeholderColor release];
[super dealloc];
}


#pragma mark -
#pragma mark UIView

- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_textChanged:) name:UITextViewTextDidChangeNotification object:self];

self.placeholderColor = [UIColor colorWithWhite:0.702f alpha:1.0f];
_shouldDrawPlaceholder = NO;
}
return self;
}


- (void)drawRect:(CGRect)rect {
[super drawRect:rect];

if (_shouldDrawPlaceholder) {
[_placeholderColor set];
[_placeholder drawInRect:CGRectMake(8.0f, 8.0f, self.frame.size.width - 16.0f, self.frame.size.height - 16.0f) withFont:self.font];
}
}


#pragma mark -
#pragma mark Private Methods

- (void)_updateShouldDrawPlaceholder {
BOOL prev = _shouldDrawPlaceholder;
_shouldDrawPlaceholder = self.placeholder && self.placeholderColor && self.text.length == 0;

if (prev != _shouldDrawPlaceholder) {
[self setNeedsDisplay];
}
}


- (void)_textChanged:(NSNotification *)notificaiton {
[self _updateShouldDrawPlaceholder];
}

@end
Something went wrong with that request. Please try again.