码迷,mamicode.com
首页 > 其他好文 > 详细

UITextView占位符

时间:2016-08-01 12:13:56      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

方法一:

  直接在textView上添加label,然后用 textViewDidChange:监听textView的文字长度,根据textView的长度等于0或者大于零来隐藏和显示Label

#pragma mark - textView占位符1
-(void)setupTextView{
    
    //定义UITextView
    UITextView *textView = [[UITextView alloc] init];
    textView.font = [UIFont systemFontOfSize:14];
    textView.frame =CGRectMake(10, 60, 300, 40);
    textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    textView.backgroundColor = [UIColor whriteColor];
    _textView = textView;
    [self.view addSubview:textView];
    textView.delegate = self;
    
    //在UITextView上面覆盖个UILable,UILable设置为全局变量。
    UILabel *label = [[UILabel alloc]init];
    label.frame =CGRectMake(10, 5, 280, 20);
    label.text = @"请输入手机号码...";
    label.enabled = NO;//lable必须设置为不可用
    label.backgroundColor = [UIColor clearColor];
    _label = label;
    [textView addSubview:label];
}
//实现UITextView的代理
-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        _label.text = @"请输入手机号码...";
    }else{
        _label.text = @"";
    } 
}

 

方法二:

自定义 WXPlaceHolderTextView继承自UITextView

//
//  WXPlaceHolderTextView.h
//  test
//
//  Created by wangxu on 16/8/1.
//  Copyright © 2016年 wangxu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface WXPlaceHolderTextView : 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

 

//
//  WXPlaceHolderTextView.m
//  test
//
//  Created by wangxu on 16/8/1.
//  Copyright © 2016年 wangxu. All rights reserved.
//

#import "WXPlaceHolderTextView.h"


@implementation WXPlaceHolderTextView

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

- (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];
}

- (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];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

 

使用:

#pragma mark - textView占位符2
-(void)setupWXTextView{
    
    //首先定义UITextView
    WXPlaceHolderTextView *textView = [[WXPlaceHolderTextView alloc] init];
    textView.font = [UIFont systemFontOfSize:14];
    textView.frame =CGRectMake(10, 60, 300, 40);
    textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    textView.backgroundColor = [UIColor whiteColor];
    _mytextView = textView;
    //设置textView的placeholder的文字和字体颜色
    textView.placeholder = @"真好用~ ~ ~";
    textView.placeholderColor = [UIColor redColor];
    [self.view addSubview:textView];
    textView.hidden = NO;
    textView.delegate = self;
}

//隐藏键盘,实现UITextViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if ([text isEqualToString:@"\n"]) {
        [_textView resignFirstResponder];
        return NO;
    }
    return YES;
}

 

UITextView占位符

标签:

原文地址:http://www.cnblogs.com/somebodywx/p/5725230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!