Created
November 13, 2012 17:45
-
-
Save dbrajkovic/4067242 to your computer and use it in GitHub Desktop.
VITextView
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
// | |
// VITextView.h | |
// | |
// Created by Dan Brajkovic on 11/13/12. | |
// Copyright (c) 2012 VOKAL Interactive. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface VITextView : UITextView | |
@property (strong, nonatomic) NSString *placeholder; | |
@property (strong, nonatomic) UIImage *background; | |
@property (strong, nonatomic) UIImage *activeBackground; | |
@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
// | |
// VITextView.m | |
// | |
// Created by Dan Brajkovic on 11/13/12. | |
// Copyright (c) 2012 VOKAL Interactive. All rights reserved. | |
// | |
#import "VITextView.h" | |
@interface VITextView () | |
@property (strong, nonatomic) UILabel *placeholderLabel; | |
@property (strong, nonatomic) UIImageView *backgroundImageView; | |
- (void)registerWithDefaultNotifcationCenter; | |
- (void)didBeginEditingText:(NSNotification *)notification; | |
- (void)didChangeText:(NSNotification *)notification; | |
- (void)didEndEditingText:(NSNotification *)notification; | |
@end | |
@implementation SPLTextView | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
[self registerWithDefaultNotifcationCenter]; | |
} | |
return self; | |
} | |
- (id)initWithCoder:(NSCoder *)aDecoder | |
{ | |
self = [super initWithCoder:aDecoder]; | |
if (self) { | |
[self registerWithDefaultNotifcationCenter]; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
- (void)registerWithDefaultNotifcationCenter | |
{ | |
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; | |
[defaultCenter addObserver:self selector:@selector(didBeginEditingText:) name:UITextViewTextDidBeginEditingNotification object:self]; | |
[defaultCenter addObserver:self selector:@selector(didChangeText:) name:UITextViewTextDidChangeNotification object:self]; | |
[defaultCenter addObserver:self selector:@selector(didEndEditingText:) name:UITextViewTextDidEndEditingNotification object:self]; | |
} | |
- (void)didBeginEditingText:(NSNotification *)notification | |
{ | |
self.placeholderLabel.hidden = self.hasText; | |
self.backgroundImageView.highlighted = YES; | |
} | |
- (void)didChangeText:(NSNotification *)notification | |
{ | |
self.placeholderLabel.hidden = self.hasText; | |
} | |
- (void)didEndEditingText:(NSNotification *)notification | |
{ | |
self.placeholderLabel.hidden = self.hasText; | |
self.backgroundImageView.highlighted = NO; | |
} | |
/* | |
// Only override drawRect: if you perform custom drawing. | |
// An empty implementation adversely affects performance during animation. | |
- (void)drawRect:(CGRect)rect | |
{ | |
// Drawing code | |
} | |
*/ | |
- (void)setPlaceholder:(NSString *)placeholder | |
{ | |
_placeholder = placeholder; | |
[self setNeedsLayout]; | |
} | |
- (void)setBackground:(UIView *)background | |
{ | |
_background = background; | |
[self setNeedsLayout]; | |
} | |
- (void)setActiveBackgroundView:(UIView *)activeBackground | |
{ | |
_activeBackground = activeBackground; | |
[self setNeedsLayout]; | |
} | |
- (void)layoutSubviews | |
{ | |
if (!self.placeholderLabel) { | |
CGRect frame = CGRectMake(8.0, 8.0, 0.0, 0.0); | |
self.placeholderLabel = [[UILabel alloc] initWithFrame:frame]; | |
[self addSubview:self.placeholderLabel]; | |
[self sendSubviewToBack:self.placeholderLabel]; | |
self.placeholderLabel.textColor = [UIColor lightGrayColor]; | |
self.placeholderLabel.backgroundColor = [UIColor clearColor]; | |
} | |
self.placeholderLabel.text = self.placeholder; | |
self.placeholderLabel.font = self.font; | |
[self.placeholderLabel sizeToFit]; | |
if (!self.backgroundImageView) { | |
self.backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectZer]o; | |
[self addSubview:self.backgroundImageView]; | |
[self sendSubviewToBack:self.backgroundImageView]; | |
} | |
[self.backgroundImageView setImage:background]; | |
[self.backgroundImageView setHighlightedImage:activeBackground]; | |
if (!activeBackground && background) { | |
[self.backgroundImageView setHighlightedImage:background] | |
} | |
self.backgroundImageView.frame = CGRectMake(0, self.contentOffset.y, self.frame.size.width, self.frame.size.height); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment