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]];
}
}
相关新闻>>
- 发表评论
-
- 最新评论 进入详细评论页>>









![[iPhone中级]iPhone团购信息客户端的开发 (二)](/uploads/allimg/131022/200H11235-0-lp.png)
