UITextView实现PlaceHolder的方式
生活随笔
收集整理的這篇文章主要介紹了
UITextView实现PlaceHolder的方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實現UITextView實現PlaceHolder的方式的方式有兩種,這兩種方法的核心就是通過通知來添加和去除PlaceHolder;下面來介紹兩種方法;個人比較喜歡第一種,看起來更加合理。
方法1:原理是通過通知來改變PlaceHolder,把PlaceHolder看成是一個UILabel,設置UILabel的透明度,來讓Placeholder顯示與不顯示。這種方法對UITextView本身影響較小。學習自Fly_Elephant:《UITextView實現PlaceHolder的方式》這篇文章
.h文件
#import <UIKit/UIKit.h>@interface DLTextView : UITextView@property (nonatomic, retain) NSString *placeholder;@property (nonatomic, retain) UIColor *placeholderColor;- (void)textChanged:(NSNotification*)notification;@end.m文件
#import "DLTextView.h"@interface DLTextView () @property (nonatomic, retain) UILabel *placeHolderLabel;@end@implementation DLTextViewCGFloat 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.font = [UIFont systemFontOfSize:13.0];_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第二種方法:原理是通過Placeholder字符串的長度,當textView輸入內容時,Placeholder 字符串的長度為nil,當textView不輸入內容時,Placeholder顯示。
#import <UIKit/UIKit.h>@interface DLTextView : UITextView@property (nonatomic, copy) NSString *placeholder;@end#import "DLTextView.h"@implementation DLTextView- (instancetype)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {[self setup];}return self; } - (instancetype)initWithCoder:(NSCoder *)decoder {self = [super initWithCoder:decoder];if (self) {[self setup];}return self; } - (void)awakeFromNib { // [self setup];}- (void)setup { // NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:super.text]; // NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; // paragraphStyle.lineSpacing = 10; // // [attributedText addAttributes:@{NSParagraphStyleAttributeName : paragraphStyle} range:NSMakeRange(0, super.text.length)]; // // super.attributedText = attributedText;// 添加通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil]; } - (void)dealloc {[[NSNotificationCenter defaultCenter] removeObserver:self]; } #pragma mark - 開始編輯的消息方法 - (void)textViewDidBeginEditing:(NSNotification *)sender {// 開始編輯的時候,父控件的text如果等于placeholder就讓什么也不顯示if ([super.text isEqualToString:self.placeholder]) {super.text = @"";[super setTextColor:[UIColor blackColor]];}}- (void)textViewDidEndEditing:(NSNotification *)sender {if (super.text.length == 0) {super.text = self.placeholder;[super setTextColor:[UIColor grayColor]];} } - (void)setPlaceholder:(NSString *)placeholder {_placeholder = placeholder;// 調用通知的方法,讓placeholder顯示在UI上面[self textViewDidEndEditing:nil]; } #pragma mark - 重寫父類的text方法 - (NSString *)text {if ([super.text isEqualToString:self.placeholder]) {super.text = @"";}return super.text; } @end
轉載于:https://www.cnblogs.com/peaker-wu/p/5486176.html
總結
以上是生活随笔為你收集整理的UITextView实现PlaceHolder的方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS开发基础知识--碎片39
- 下一篇: 5月12日