#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
#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]];
}
}
在ViewController中用時,1、在xib上加一個uitextview連接上,2、直接創建initframe 但是不要忘了,placeholder,和添加通知,移除通知。
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.placeholder = @"請添寫你的信息……";
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
[self.textView addObserver];
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:YES];
[self.textView removeobserver];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.placeholder = @"請添寫你的信息……";
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
[self.textView addObserver];
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:YES];
[self.textView removeobserver];
}
效果圖: