iOS 客户端学习-UITextView

来源:互联网 发布:visio 网络拓扑图 注意 编辑:程序博客网 时间:2024/05/17 02:19

1.UITextView中添加placeholder

   创建GCPlaceholderTextView类,如下所示,使用方法,创建公共方法

   

   +(void)setTextViewByTextView:(GCPlaceholderTextView*)textView placeholder:        (NSString*)placeholder{

    textView.placeholderColor = [UIColorlightGrayColor];

    textView.placeholder =NSLocalizedString(placeholder,);

    }

在页面调用方法如下:

   

   [UtilssetTextViewByTextView:self.textView placeholder:@""];

    

GCPlaceholderTextView.h文件

    

#import <UIKit/UIKit.h>



@interface GCPlaceholderTextView : UITextView 


@property(nonatomic,strong) NSString *placeholder;


@property (nonatomic,strong) UIColor *realTextColorUI_APPEARANCE_SELECTOR;

@property (nonatomic,strong) UIColor *placeholderColorUI_APPEARANCE_SELECTOR;


@end


GCPlaceholderTextView.m文件

#import "GCPlaceholderTextView.h"


@interface GCPlaceholderTextView () 


@property (unsafe_unretained,nonatomic, readonly)NSString* realText;


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

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


@end


@implementation GCPlaceholderTextView


@synthesize realTextColor;

@synthesize placeholder;

@synthesize placeholderColor;


#pragma mark -

#pragma mark Initialisation


- (id) initWithFrame:(CGRect)frame {

   if ((self = [superinitWithFrame:frame])) {

        [selfawakeFromNib];

    }

    return self;

}


- (void)awakeFromNib {

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(beginEditing:)name:UITextViewTextDidBeginEditingNotificationobject:self];

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(endEditing:)name:UITextViewTextDidEndEditingNotificationobject:self];

    

    self.realTextColor =self.textColor;

    self.placeholderColor = [UIColorlightGrayColor];

}


#pragma mark -

#pragma mark Setter/Getters


- (void) setPlaceholder:(NSString *)aPlaceholder {

    if ([self.realTextisEqualToString:placeholder] && ![selfisFirstResponder]) {

       self.text = aPlaceholder;

    }

   if (aPlaceholder != placeholder) {

       placeholder = aPlaceholder;

    }

    

    

    [selfendEditing:nil];

}


- (void)setPlaceholderColor:(UIColor *)aPlaceholderColor {

   placeholderColor = aPlaceholderColor;

    

   if ([super.textisEqualToString:self.placeholder]) {

        self.textColor =self.placeholderColor;

    }

}


- (NSString *) text {

   NSString* text = [supertext];

   if ([text isEqualToString:self.placeholder])return @"";

   return text;

}


- (void) setText:(NSString *)text {

   if (([text isEqualToString:@""] || text ==nil) && ![selfisFirstResponder]) {

       super.text =self.placeholder;

    }

   else {

       super.text = text;

    }

    

   if ([text isEqualToString:self.placeholder] || text ==nil) {

        self.textColor =self.placeholderColor;

    }

   else {

       self.textColor =self.realTextColor;

    }

}


- (NSString *) realText {

    return [supertext];

}


- (void) beginEditing:(NSNotification*) notification {

    if ([self.realTextisEqualToString:self.placeholder]) {

       super.text =nil;

       self.textColor =self.realTextColor;

    }

}


- (void) endEditing:(NSNotification*) notification {

   if ([self.realTextisEqualToString:@""] ||self.realText ==nil) {

       super.text =self.placeholder;

        self.textColor =self.placeholderColor;

    }

}


- (void) setTextColor:(UIColor *)textColor {

    if ([self.realTextisEqualToString:self.placeholder]) {

       if ([textColor isEqual:self.placeholderColor]){

             [supersetTextColor:textColor];

        }else {

           self.realTextColor = textColor;

        }

    }

   else {

       self.realTextColor = textColor;

        [supersetTextColor:textColor];

    }

}


#pragma mark -

#pragma mark Dealloc


- (void)dealloc {

    [[NSNotificationCenterdefaultCenter] removeObserver:self];

    

}


@end



0 0
原创粉丝点击