UITexFieldには簡単にプレースホルダが設定できるのに、UITextViewにはないんだよね!

本当困っちゃう!(`Δ´)

調べるとみなさん自作のUITextView作ってますね!
色々なサイトを参考に私も作ってみました。

#import < UIKit/UIKit.h>

@interface UIPlaceHolderTextView : UITextView {
NSString *placeholder;
UIColor *placeholderColor;

@private
UILabel *placeHolderLabel;
}

@property (nonatomic, retain) UILabel *placeHolderLabel;
@property (nonatomic, retain) NSString *placeholder;
@property (nonatomic, retain) UIColor *placeholderColor;

-(void)textChanged:(NSNotification*)notification;

@end


#import "UIPlaceHolderTextView.h"

@implementation UIPlaceHolderTextView

@synthesize placeHolderLabel;
@synthesize placeholder;
@synthesize placeholderColor;

NSString *placeTxt =@"ここにテキストメモが入力できます。";

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setPlaceholder:placeTxt];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];

}
return self;
}



- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
#if __has_feature(objc_arc)
#else
[placeHolderLabel release]; placeHolderLabel = nil;
[placeholderColor release]; placeholderColor = nil;
[placeholder release]; placeholder = nil;
[super dealloc];
#endif

}

- (void)awakeFromNib
{
[super awakeFromNib];
[self setPlaceholder:placeTxt];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
}


- (void)textChanged:(NSNotification *)notification
{
if([[self placeholder] length] == 0)
{
return;
}

if([[self text] length] == 0)
{
[[self viewWithTag:999] setAlpha:1];
}
else
{
[[self viewWithTag:999] setAlpha:0];
}
}

- (void)setText:(NSString *)text {
[super setText:text];
[self textChanged:nil];
}

- (void)drawRect:(CGRect)rect
{
if( [[self placeholder] length] > 0 )
{
if ( placeHolderLabel == nil )
{
placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];
placeHolderLabel.lineBreakMode = UILineBreakModeWordWrap;
placeHolderLabel.numberOfLines = 0;
placeHolderLabel.font = self.font;
placeHolderLabel.backgroundColor = [UIColor clearColor];
placeHolderLabel.textColor = self.placeholderColor;
placeHolderLabel.alpha = 0;
placeHolderLabel.tag = 999;
[self addSubview:placeHolderLabel];
}

placeHolderLabel.text = self.placeholder;
[placeHolderLabel sizeToFit];
[self sendSubviewToBack:placeHolderLabel];
}

if( [[self text] length] == 0 && [[self placeholder] length] > 0 )
{
[[self viewWithTag:999] setAlpha:1];
}

[super drawRect:rect];
}

@end

てな具合です。

ほとんどコピペです。
とても簡単に使わせてもらいました。
これでプレースホルダっぽくなりました。

文字が入力されたらlabelを透明化してるんですね!なるほど。勉強になりました。

あざーす。