#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
{
[[NSNotificationCenterdefaultCenter] removeObserver:self];
[placeHolderLabel release];placeHolderLabel = nil;
[placeholderColor release];placeholderColor = nil;
[placeholder release];placeholder = nil;
[super dealloc];
}
- (void)awakeFromNib
{
[super awakeFromNib];
[selfsetPlaceholder:@""];
[selfsetPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenterdefaultCenter] addObserver:self selector:@selector(textChanged:)name:UITextViewTextDidChangeNotification object:nil];
}
- (id)initWithFrame:(CGRect)frame
{
if( (self = [superinitWithFrame:frame]) )
{
[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColorlightGrayColor]];
[[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;
[selfaddSubview: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 *)textViewshouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
if ([textisEqualToString:@"\n"]) {
[m_textView resignFirstResponder];
return NO;
}
return YES;
}
#pragma ################# textView delegate ###################
- (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
if(1== range.length) {
//按下回格键
returnYES;
}
if([text isEqualToString:@"\n"]) {//按下return键
//这里隐藏键盘,不做任何处理
[textView resignFirstResponder];
returnNO;
}else{
if([textView.textlength] < 30) {//判断字符个数
returnYES;
}
}
returnNO;
}
- (BOOL)textViewShouldBeginEditing:(UITextView*)textView
{
if(_is_first_load){
textView.text=nil;
_is_first_load=false;
}
returnYES;
}
- (void)textViewDidChange:(UITextView*)textView
{
intlenth=[textView.textlength];//字数
NSString*_txt_conten=nil;
if(lenth>30){
_txt_conten=[textView.textsubstringToIndex:30];
}
else{
_txt_conten=textView.text;
}
textView.text=_txt_conten;
NSString*txt_length=[NSStringstringWithFormat:@"%d/30",_txt_conten.length];
lbl_count.text=txt_length;
}