首页 > 代码库 > iOS疯狂详解之UITextView加上占位字符

iOS疯狂详解之UITextView加上占位字符

//
//  SSTextView.h
//  SSToolkit
//
//  Created by Sam Soffes on 8/18/10.
//  Copyright 2010-2011 Sam Soffes. All rights reserved.
//
 
/**
 UITextView subclass that adds placeholder support like UITextField has.
 */
@interfaceSSTextView : UITextView
 
/**
 The string that is displayed when there is no other text in the text view.
 
 The default value is `nil`.
 */
@property (nonatomic, retain)NSString *placeholder;
 
/**
 The color of the placeholder.
 
 The default is `[UIColor lightGrayColor]`.
 */
@property (nonatomic, retain)UIColor *placeholderColor;
 
@end
 
 
//
//  SSTextView.m
//  SSToolkit
//
//  Created by Sam Soffes on 8/18/10.
//  Copyright 2010-2011 Sam Soffes. All rights reserved.
//
 
#import "SSTextView.h"
 
@interfaceSSTextView ()
- (void)_initialize;
- (void)_updateShouldDrawPlaceholder;
- (void)_textChanged:(NSNotification*)notification;
@end
 
 
@implementationSSTextView {
    BOOL_shouldDrawPlaceholder;
}
 
 
#pragma mark - Accessors
 
@synthesizeplaceholder = _placeholder;
@synthesizeplaceholderColor = _placeholderColor;
 
- (void)setText:(NSString*)string {
    [supersetText:string];
    [self_updateShouldDrawPlaceholder];
}
 
 
- (void)setPlaceholder:(NSString*)string {
    if([string isEqual:_placeholder]) {
        return;
    }
 
    [_placeholderrelease];
    _placeholder = [stringretain];
 
    [self_updateShouldDrawPlaceholder];
}
 
 
#pragma mark - NSObject
 
- (void)dealloc {
    [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UITextViewTextDidChangeNotificationobject:self];
 
    [_placeholderrelease];
    [_placeholderColorrelease];
    [superdealloc];
}
 
 
#pragma mark - UIView
 
- (id)initWithCoder:(NSCoder*)aDecoder {
    if((self = [super initWithCoder:aDecoder])) {
        [self_initialize];
    }
    returnself;
}
 
 
- (id)initWithFrame:(CGRect)frame {
    if((self = [super initWithFrame:frame])) {
        [self_initialize];
    }
    returnself;
}
 
 
- (void)drawRect:(CGRect)rect {
    [superdrawRect:rect];
 
    if(_shouldDrawPlaceholder) {
        [_placeholderColorset];
        [_placeholderdrawInRect:CGRectMake(8.0f,8.0f,self.frame.size.width- 16.0f,self.frame.size.height- 16.0f)withFont:self.font];
    }
}
 
 
#pragma mark - Private
 
- (void)_initialize {
    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(_textChanged:)name:UITextViewTextDidChangeNotificationobject:self];
 
    self.placeholderColor= [UIColor colorWithWhite:0.702falpha:1.0f];
    _shouldDrawPlaceholder =NO;
}
 
 
- (void)_updateShouldDrawPlaceholder {
    BOOLprev = _shouldDrawPlaceholder;
    _shouldDrawPlaceholder =self.placeholder&& self.placeholderColor&& self.text.length== 0;
 
    if(prev != _shouldDrawPlaceholder) {
        [selfsetNeedsDisplay];
    }
}
 
 
- (void)_textChanged:(NSNotification*)notificaiton {
    [self_updateShouldDrawPlaceholder];   
}
 

@end

另外一种实现方式。

#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
 
//.m file
#import "UIPlaceHolderTextView.h"
 
 
@implementationUIPlaceHolderTextView
 
@synthesizeplaceHolderLabel;
@synthesizeplaceholder;
@synthesizeplaceholderColor;
 
- (void)dealloc
{
    [[NSNotificationCenterdefaultCenter] removeObserver:self];
    [placeHolderLabelrelease]; placeHolderLabel =nil;
    [placeholderColorrelease]; placeholderColor =nil;
    [placeholderrelease]; placeholder =nil;
    [superdealloc];
}
 
- (void)awakeFromNib
{
    [superawakeFromNib];
    [selfsetPlaceholder:@""];
    [selfsetPlaceholderColor:[UIColorlightGrayColor]];
    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(textChanged:)name:UITextViewTextDidChangeNotificationobject:nil];
}
 
- (id)initWithFrame:(CGRect)frame
{
    if( (self= [super initWithFrame:frame]) )
    {
        [selfsetPlaceholder:@""];
        [selfsetPlaceholderColor:[UIColorlightGrayColor]];
        [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(textChanged:)name:UITextViewTextDidChangeNotificationobject:nil];
    }
    returnself;
}
 
- (void)textChanged:(NSNotification*)notification
{
    if([[selfplaceholder] length] == 0)
    {
        return;
    }
 
    if([[selftext] length] == 0)
    {
        [[selfviewWithTag:999]setAlpha:1];
    }
    else
    {
        [[selfviewWithTag:999]setAlpha:0];
    }
}
 
- (void)setText:(NSString*)text {
    [supersetText:text];
    [selftextChanged:nil];
}
 
- (void)drawRect:(CGRect)rect
{
    if( [[selfplaceholder] length] > 0 )
    {
        if( placeHolderLabel == nil)
        {
            placeHolderLabel = [[UILabelalloc] 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;
        [placeHolderLabelsizeToFit];
        [selfsendSubviewToBack:placeHolderLabel];
    }
 
    if( [[selftext] length] == 0 && [[self placeholder]length] > 0)
    {
        [[selfviewWithTag:999]setAlpha:1];
    }
 
    [superdrawRect:rect];
}
 
@end

iOS疯狂详解之UITextView加上占位字符