@blog.justoneplanet.info

日々勉強

UITextViewをEditableにしてplaceholderを実装する

UITextFieldでは以下のようにすることでプレースホルダーを使用することができる。

UITextField *field = [[UITextField alloc] init];
[field setFrame:CGRectMake(10, 120, 190, 35)];
[field setPlaceholder:@"入力してください"];

複数行のテキスト入力欄が必要な場合はUITextViewをEditableにして対応するが、上述のような手法でプレースホルダーを用いることはできない。

■実装

UIPlaceHolderTextView.h

#import "UIPlaceHolderTextView.h"

@implementation UIPlaceHolderTextView
@synthesize placeholder;
@synthesize placeholderColor;
@synthesize placeholderLabel;

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [placeholderLabel release]; placeholderLabel = nil;
    [placeholderColor release]; placeholderColor = nil;
    [placeholder release]; placeholder = nil;
    [super dealloc];
}

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

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

// 表示テキストに変更があった時
- (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];
}

// drawRect時にplaceholderのUILabelを準備してViewに追加する
- (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

クライアントコード

以下のようにすることで使用することができる。

UIPlaceHolderTextView *view = [[UIPlaceHolderTextView alloc] init];
[view setPlaceholder:@"(^o^)"];

■参考

1件のコメント»

In Arabic, the placeholder do not display from right to left. I’ve tried to set text alignment but did not have any effect. Can you solve it?

RSS feed for comments on this post.TrackBack URL

Leave a comment