iphone自定义UITextView的placeholder

来源:未知 责任编辑:责任编辑 发表时间:2014-02-18 03:21 点击:
大家都知道UITextField才有placeholder属性,UITextView 并没有placeholder,那么怎么模拟UITextfield使UITextView也有placeholder。 p> 

p>思路是:继承uitextview,判断当text为空时就让[super text]显示placeholder。

p> 

p>代码如下(我引用了arc):

p> 

p> 

p>UIPlaceholderTextView.h

p> 

p> 

p> 
#import <UIKit/UIKit.h>   
  
@interface UIPlaceholderTextView : UITextView  
@property(nonatomic, strong) NSString *placeholder;     //占位符   
  
-(void)addObserver;//添加通知   
-(void)removeobserver;//移除通知   
@end  

#import <UIKit/UIKit.h>

@interface UIPlaceholderTextView : UITextView
@property(nonatomic, strong) NSString *placeholder;     //占位符

-(void)addObserver;//添加通知
-(void)removeobserver;//移除通知
@end
UIPlaceholderTextView.m

 

p> 

p> 

p> 

p> 
#import "UIPlaceholderTextView.h"   
  
@interface UIPlaceholderTextView ()  
  
@property (nonatomic, strong) UIColor* textColor;  
  
- (void) beginEditing:(NSNotification*) notification;  
- (void) endEditing:(NSNotification*) notification;  
  
@end  
  
@implementation UIPlaceholderTextView  
@synthesize placeholder;  
@synthesize textColor;  
- (id) initWithFrame:(CGRect)frame {  
    if ((self = [super initWithFrame:frame])) {  
        [self awakeFromNib];  
    }  
    return self;  
}  
//当用nib创建时会调用此方法   
- (void)awakeFromNib {  
    textColor = [UIColor redColor];  
    [self addObserver];  
}  
-(void)addObserver  
{  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing:) name:UITextViewTextDidBeginEditingNotification object:self];  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing:) name:UITextViewTextDidEndEditingNotification object:self];  
}  
-(void)removeobserver  
{  
    [[NSNotificationCenter defaultCenter] removeObserver:self];  
}  
  
#pragma mark -   
#pragma mark Setter/Getters   
- (void) setPlaceholder:(NSString *)aPlaceholder {  
    placeholder = aPlaceholder;  
    [self endEditing:nil];  
}  
  
- (NSString *) text {  
    NSString* text = [super text];  
    if ([text isEqualToString:placeholder]) return @"";  
    return text;  
}  
  
- (void) beginEditing:(NSNotification*) notification {  
    if ([super.text isEqualToString:placeholder]) {  
        super.text = nil;  
        //字体颜色   
        [super setTextColor:textColor];  
    }  
      
}  
  
- (void) endEditing:(NSNotification*) notification {  
    if ([super.text isEqualToString:@""] || self.text == nil) {  
        super.text = placeholder;  
        //注释颜色   
        [super setTextColor:[UIColor lightGrayColor]];  
    }  
}  

#import "UIPlaceholderTextView.h"

@interface UIPlaceholderTextView ()

@property (nonatomic, strong) UIColor* textColor;

- (void) beginEditing:(NSNotification*) notification;
- (void) endEditing:(NSNotification*) notification;

@end

@implementation UIPlaceholderTextView
@synthesize placeholder;
@synthesize textColor;
- (id) initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self awakeFromNib];
    }
    return self;
}
//当用nib创建时会调用此方法
- (void)awakeFromNib {
    textColor = [UIColor redColor];
    [self addObserver];
}
-(void)addObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing:) name:UITextViewTextDidBeginEditingNotification object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing:) name:UITextViewTextDidEndEditingNotification object:self];
}
-(void)removeobserver
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -
#pragma mark Setter/Getters
- (void) setPlaceholder:(NSString *)aPlaceholder {
    placeholder = aPlaceholder;
    [self endEditing:nil];
}

- (NSString *) text {
    NSString* text = [super text];
    if ([text isEqualToString:placeholder]) return @"";
    return text;
}

- (void) beginEditing:(NSNotification*) notification {
    if ([super.text isEqualToString:placeholder]) {
        super.text = nil;
        //字体颜色
        [super setTextColor:textColor];
    }
    
}

- (void) endEditing:(NSNotification*) notification {
    if ([super.text isEqualToString:@""] || self.text == nil) {
        super.text = placeholder;
        //注释颜色
        [super setTextColor:[UIColor lightGrayColor]];
    }
}
	
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • Lexical or Preprocessor Issue 'xxx.h
  • ios学习笔记(二)xcode 4.3.2下实现基本交互
  • ios版本的helloworld
  • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
  • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
  • UITableView一些方法
  • IOS类似iphone通讯录TableView的完整demo【附源码】
  • [iPhone中级]iPhone团购信息客户端的开发 (二)
  • iphone(object-c)内存管理(1)
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1