Permalink
Browse files

Fixes #2 rotation issue.

This fixes the rotation issue where the placeholder text would moving
out of bounds when the device is rotated. Also fixed a memory leak
found in -setup. And refactored out two ivars.
  • Loading branch information...
1 parent 9860d10 commit 046e1a43ede2a53f67ae09611767dcd6f3bc8e3c @kirbyt committed Jun 1, 2012
Showing with 46 additions and 23 deletions.
  1. +0 −2 Classes/KTTextView.h
  2. +46 −21 Classes/KTTextView.m
View
@@ -30,8 +30,6 @@
@interface KTTextView : UITextView
{
- NSString *_placeholderText;
- UIColor *_placeholderColor;
UILabel *_placeholder;
}
View
@@ -33,44 +33,41 @@ @interface KTTextView ()
@implementation KTTextView
-@synthesize placeholderText = _placeholderText;
-@synthesize placeholderColor = _placeholderColor;
+//@synthesize placeholderText = _placeholderText;
+//@synthesize placeholderColor = _placeholderColor;
@synthesize placeholder = _placeholder;
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
- [_placeholderText release], _placeholderText = nil;
- [_placeholderColor release], _placeholderColor = nil;
[_placeholder release], _placeholder = nil;
[super dealloc];
}
- (void)setup
{
- [self setPlaceholderText:@""];
- [self setPlaceholderColor:[UIColor lightGrayColor]];
-
if ([self placeholder]) {
[[self placeholder] removeFromSuperview];
[self setPlaceholder:nil];
}
- CGRect frame = CGRectMake(8, 8, self.bounds.size.width - 16, 0);
+ CGRect frame = CGRectMake(8, 8, self.bounds.size.width - 16, 0.0);
UILabel *placeholder = [[UILabel alloc] initWithFrame:frame];
[placeholder setLineBreakMode:UILineBreakModeWordWrap];
[placeholder setNumberOfLines:0];
[placeholder setBackgroundColor:[UIColor clearColor]];
- [placeholder setAlpha:0];
- [placeholder setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
+ [placeholder setAlpha:1.0];
+ [placeholder setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
+ [placeholder setTextColor:[UIColor lightGrayColor]];
+ [placeholder setText:@""];
[self addSubview:placeholder];
- [placeholder sizeToFit];
[self sendSubviewToBack:placeholder];
[self setPlaceholder:placeholder];
-
+ [placeholder release];
+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getFocus:) name:UITextViewTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(lostFocus:) name:UITextViewTextDidEndEditingNotification object:nil];
@@ -102,7 +99,7 @@ - (id)initWithFrame:(CGRect)frame
- (void)textChanged:(NSNotification *)notification
{
- if ([_placeholderText length] == 0) {
+ if ([[_placeholder text] length] == 0) {
return;
}
@@ -129,17 +126,45 @@ - (void)lostFocus:(NSNotification *)notification
- (void)drawRect:(CGRect)rect
{
- if ([_placeholderText length] > 0) {
+ [super drawRect:rect];
+ if ([[self text] length] == 0 && [[_placeholder text] length] > 0) {
+ [_placeholder setAlpha:1.0];
+ } else {
[_placeholder setAlpha:0.0];
- [_placeholder setFont:[self font]];
- [_placeholder setTextColor:_placeholderColor];
- [_placeholder setText:_placeholderText];
- [_placeholder sizeToFit];
}
+}
+
+- (void)setFont:(UIFont *)font
+{
+ [super setFont:font];
+ [_placeholder setFont:font];
+}
+
+- (NSString *)placeholderText
+{
+ return [_placeholder text];
+}
+
+- (void)setPlaceholderText:(NSString *)placeholderText
+{
+ [_placeholder setText:placeholderText];
- if ([[self text] length] == 0 && [_placeholderText length] > 0) {
- [_placeholder setAlpha:1.0];
- }
+ CGRect frame = _placeholder.frame;
+ CGSize constraint = CGSizeMake(frame.size.width, 42.0f);
+ CGSize size = [placeholderText sizeWithFont:[self font] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
+
+ frame.size.height = size.height;
+ [_placeholder setFrame:frame];
+}
+
+- (UIColor *)placeholderColor
+{
+ return [_placeholder textColor];
+}
+
+- (void)setPlaceholderColor:(UIColor *)placeholderColor
+{
+ [_placeholder setTextColor:placeholderColor];
}
@end

0 comments on commit 046e1a4

Please sign in to comment.