你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iphone自定義UITextView的placeholder

iphone自定義UITextView的placeholder

編輯:IOS開發綜合
大家都知道UITextField才有placeholder屬性,UITextView 並沒有placeholder,那麼怎麼模擬UITextfield使UITextView也有placeholder。   思路是:繼承uitextview,判斷當text為空時就讓[super text]顯示placeholder。   代碼如下(我引用了arc):     UIPlaceholderTextView.h      
#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];
}

 

        效果圖:         \   \          
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved