关闭

UITextView 实现placeholder的方法

3837人阅读 评论(0) 收藏 举报

转载自:http://www.cnblogs.com/easonoutlook/archive/2012/12/28/2837665.html

(我用的是第二种方法继承,代码没动,倒也实现了,有空了再好好研究下)

在UITextField中自带placeholder属性,可以用于提示输入框信息。但是UITextView并不具备此功能

介绍两种方法来实现:

第一种:

初始化UITextView

  1. //首先定义UITextView  
  2. UITextView *textView = [[UITextView alloc] init];  
  3. textView.font = [UIFont systemFontOfSize:14];  
  4. textView.frame =CGRectMake(10, 0, cell.contentView.bounds.size.width-20, side);  
  5. textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;  
  6. textView.backgroundColor = [UIColor whiteColor];  
  7. [cell.contentView addSubview:textView];  
  8. textView.hidden = NO;  
  9. textView.delegate = self;  
  10. //其次在UITextView上面覆盖个UILable,UILable设置为全局变量。  
  11. uilabel.frame =CGRectMake(17, 8, cell.contentView.bounds.size.width - side+10, 20);  
  12. uilabel.text = @"请填写审批意见...";  
  13. uilabel.enabled = NO;//lable必须设置为不可用  
  14. uilabel.backgroundColor = [UIColor clearColor];  
  15. [cell.contentView addSubview:uilabel];  

实现UITextView的代理

  1. -(void)textViewDidChange:(UITextView *)textView  
  2. {  
  3.     self.examineText =  textView.text;  
  4.     if (textView.text.length == 0) {  
  5.         uilabel.text = @"请填写审批意见...";  
  6.     }else{  
  7.         uilabel.text = @"";  
  8.     }  

 

第二种:

UITextView 实现 placeholder 及隐藏键盘

 

#import <Foundation/Foundation.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;

 

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

}

 

- (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

 

 

//隐藏键盘,实现UITextViewDelegate

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text  

{  

    if ([text isEqualToString:@"\n"]) {  

        [m_textView resignFirstResponder];   

        return NO;  

    }  

    return YES;  

}  


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2332685次
    • 积分:23502
    • 等级:
    • 排名:第293名
    • 原创:413篇
    • 转载:635篇
    • 译文:0篇
    • 评论:102条
    最新评论