標籤:
之前開發遇到過UITextField中加入一個PlaceHolder的問題,直接設定一下即可,不過這次是需要在UITextView中實現一個PlaceHolder,跟之前有點不同。在網上參考了各位前輩的解決方案,大概有兩種方式,第一種方式很猥瑣,就是直接給UITextView的text賦值,比如說預設提示是"部落格園FlyElephant",在textViewDidChange中判斷是不是“部落格園FlyElephant”,如果是就清空,如果不是就繼續提示,弊端就是使用者輸入的內容不能和你的預設提示一樣,第二種方式需要加入一個UILabel,同樣在textViewDidChange中進行判斷,一般都是這麼實現,不過有的都是建立textView中的時候建立UILabel,這樣做無可厚非,不過最好還是抽象出來。繼承UITextView擴充一下,建立一個FEPlaceHolderTextView:
標頭檔:
//// FEPlaceHolderTextView.h// MyTextViewDemo//http://www.cnblogs.com/xiaofeixiang// Created by keso on 15/5/17.// Copyright (c) 2015年 keso. All rights reserved.//#import <UIKit/UIKit.h>@interface FEPlaceHolderTextView : UITextView@property (nonatomic, retain) NSString *placeholder;@property (nonatomic, retain) UIColor *placeholderColor;-(void)textChanged:(NSNotification*)notification;@end
實現檔案:
//// FEPlaceHolderTextView.m// MyTextViewDemo//http://www.cnblogs.com/xiaofeixiang// Created by keso on 15/5/17.// Copyright (c) 2015年 keso. All rights reserved.//#import "FEPlaceHolderTextView.h"@interface FEPlaceHolderTextView ()@property (nonatomic, retain) UILabel *placeHolderLabel;@end@implementation FEPlaceHolderTextViewCGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25;- (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self];}- (void)awakeFromNib{ [super awakeFromNib]; if (!self.placeholder) { [self setPlaceholder:@""]; } if (!self.placeholderColor) { [self setPlaceholderColor:[UIColor lightGrayColor]]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];}- (id)initWithFrame:(CGRect)frame{ if( (self = [super initWithFrame:frame]) ) { [self setPlaceholder:@""]; [self setPlaceholderColor:[UIColor lightGrayColor]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; } return self;}- (void)textChanged:(NSNotification *)notification{ if([[self placeholder] length] == 0) { return; } [UIView animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION animations:^{ 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,10)]; _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping; _placeHolderLabel.numberOfLines =0; _placeHolderLabel.font = self.font; _placeHolderLabel.backgroundColor = [UIColor clearColor]; _placeHolderLabel.textColor = self.placeholderColor; _placeHolderLabel.alpha = 0; _placeHolderLabel.tag = 999; [self addSubview:_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
調用:
self.textView=[[FEPlaceHolderTextView alloc]initWithFrame:CGRectMake(10, 30, CGRectGetWidth(self.view.frame)-20, 200)]; [email protected]"部落格園FlyElephant\n部落格地址:http://www.cnblogs.com/xiaofeixiang"; self.textView.layer.borderColor=[UIColor lightGrayColor].CGColor; self.textView.layer.borderWidth=1.0; self.textView.scrollEnabled = YES; self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight; //自適應高度 self.textView.returnKeyType = UIReturnKeyDefault; //返回鍵的類型 self.textView.keyboardType = UIKeyboardTypeDefault; //鍵盤類型 [self.view addSubview:self.textView];
效果:
iOS開發-UITextView實現PlaceHolder的方式