發微博視圖,提示文字背景:app
// IWTextView.m 文件 // // IWTextView.m // ItcastWeibo // // Created by apple on 14-5-19. // Copyright (c) 2014年 itcast. All rights reserved. // #import "IWTextView.h" @interface IWTextView() @property (nonatomic,weak) UILabel *placeholderLabel; @end @implementation IWTextView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { UILabel *placeholderLabel = [[UILabel alloc] init]; placeholderLabel.textColor = [UIColor lightGrayColor]; placeholderLabel.hidden = YES; // 文本輸入框,文字提示的label背景 placeholderLabel.backgroundColor = [UIColor orangeColor]; [self addSubview:placeholderLabel]; self.placeholderLabel = placeholderLabel; } return self; } -(void)setPlaceholder:(NSString *)placeholder { self.placeholderLabel.text = placeholder; if(placeholder.length) // 是否須要顯示 { self.placeholderLabel.hidden = NO; self.placeholderLabel.frame = self.bounds; /* // 計算frame CGFloat maxH = self.frame.size.height; CGFloat maxW = self.frame.size.width; [placeholder sizeWithFont:self.placeholderLabel.font constrainedToSize:CGSizeMake(maxW, maxH)]; self.placeholderLabel.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>); */ }else{ self.placeholderLabel.hidden = YES; } self.placeholderLabel.hidden = (placeholder.length == 0); } @end
文字提示「分享新鮮事」在label的中間,怎麼讓在第一行左邊呢?字體
IWTextView.m文件:atom
-(void)setPlaceholder:(NSString *)placeholder { self.placeholderLabel.text = placeholder; if(placeholder.length) // 是否須要顯示 { self.placeholderLabel.hidden = NO; self.placeholderLabel.frame = self.bounds; // 計算frame,設置文字提示「分享新鮮事...」的顯示位置 CGFloat maxH = self.frame.size.height; CGFloat maxW = self.frame.size.width; CGSize placeholderSize = [placeholder sizeWithFont:self.placeholderLabel.font constrainedToSize:CGSizeMake(maxW, maxH)]; self.placeholderLabel.frame = CGRectMake(0, 0, placeholderSize.width, placeholderSize.height); }else{ self.placeholderLabel.hidden = YES; } self.placeholderLabel.hidden = (placeholder.length == 0); }
設置文字換行:
spa
placeholderLabel.numberOfLines = 0; // 文字換行
從上圖能夠看出,label跑到光標上邊去了,怎麼調整呢?code
設置placeholderLabel 的添加到父控制器的方式:server
IWTextView.m文件:rem
// [self addSubview:placeholderLabel]; [self insertSubview:placeholderLabel atIndex:0];
而後在設置Placeholder的方法中,設置光標的位置:it
-(void)setPlaceholder:(NSString *)placeholder { self.placeholderLabel.text = placeholder; if(placeholder.length) // 是否須要顯示 { self.placeholderLabel.hidden = NO; self.placeholderLabel.frame = self.bounds; // 計算frame,設置文字提示「分享新鮮事...」的顯示位置 CGFloat placeholderX = 5; // ******光標初始位置****** CGFloat placeholderY = 8; // ******光標初始位置****** CGFloat maxH = self.frame.size.height; CGFloat maxW = self.frame.size.width; CGSize placeholderSize = [placeholder sizeWithFont:self.placeholderLabel.font constrainedToSize:CGSizeMake(maxW, maxH)]; self.placeholderLabel.frame = CGRectMake(placeholderX, placeholderY, placeholderSize.width, placeholderSize.height); }else{ self.placeholderLabel.hidden = YES; } self.placeholderLabel.hidden = (placeholder.length == 0); }
咱們能夠看到,光標已經出如今第一行和提示文字同一行的位置,io
下面來實現當輸入文字時,提示文字消失,而當刪除文字時,又出現提示微博
實現思路:須要監聽textView文字輸入,NSNotificationCenter消息通知,當有文字輸入時隱藏placeholderLabel
具體實現代碼:
// 2.******監聽文字改變,是否隱藏placeholder****** [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
IWTextView.h頭文件
// IWTextView.h #import <UIKit/UIKit.h> @interface IWTextView : UITextView @property (nonatomic, copy)NSString *placeholder; @property (nonatomic, strong)UIColor *placeholderColor; @end
IWTextView.m文件
// // IWTextView.m // ItcastWeibo // // Created by apple on 14-5-19. // Copyright (c) 2014年 itcast. All rights reserved. // #import "IWTextView.h" @interface IWTextView() @property (nonatomic,weak) UILabel *placeholderLabel; @end @implementation IWTextView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 1.添加文字提示 UILabel *placeholderLabel = [[UILabel alloc] init]; placeholderLabel.textColor = [UIColor lightGrayColor]; placeholderLabel.hidden = YES; // 文本輸入框,文字提示的label背景 // placeholderLabel.backgroundColor = [UIColor orangeColor]; placeholderLabel.numberOfLines = 0; // 文字換行 placeholderLabel.adjustsFontSizeToFitWidth = YES; [self insertSubview:placeholderLabel atIndex:0]; // [self addSubview:placeholderLabel]; self.placeholderLabel = placeholderLabel; // 2.******監聽文字改變,是否隱藏placeholder****** [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; } return self; } -(void)setPlaceholder:(NSString *)placeholder { _placeholder = [placeholder copy]; // 保存數據到模型 self.placeholderLabel.text = placeholder; if(placeholder.length) // 是否須要顯示 { self.placeholderLabel.hidden = NO; self.placeholderLabel.frame = self.bounds; // 計算frame,設置文字提示「分享新鮮事...」的顯示位置 CGFloat placeholderX = 5; // 光標初始位置 CGFloat placeholderY = 8; CGFloat maxH = self.frame.size.height; CGFloat maxW = self.frame.size.width; CGSize placeholderSize = [placeholder sizeWithFont:self.placeholderLabel.font constrainedToSize:CGSizeMake(maxW, maxH)]; self.placeholderLabel.frame = CGRectMake(placeholderX, placeholderY, placeholderSize.width, placeholderSize.height); }else{ self.placeholderLabel.hidden = YES; } self.placeholderLabel.hidden = (placeholder.length == 0); } // 設置文本框顏色 -(void)setPlaceholderColor:(UIColor *)placeholderColor { _placeholder = placeholderColor; self.placeholderLabel.textColor = placeholderColor; } // 設置字體 -(void)setFont:(UIFont *)font{ [super setFont:font]; // 調用系統的字體 self.placeholderLabel.font = font; self.placeholder = self.placeholder; } -(void)textDidChange { self.placeholderLabel.hidden = (self.text.length != 0); } -(void)dealloc { // [NSNotificationCenter removeObserve:self]; } @end
效果: