日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UITextView实现PlaceHolder的方式

發(fā)布時間:2025/4/14 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UITextView实现PlaceHolder的方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

實現(xiàn)UITextView實現(xiàn)PlaceHolder的方式的方式有兩種,這兩種方法的核心就是通過通知來添加和去除PlaceHolder;下面來介紹兩種方法;個人比較喜歡第一種,看起來更加合理。

方法1:原理是通過通知來改變PlaceHolder,把PlaceHolder看成是一個UILabel,設(shè)置UILabel的透明度,來讓Placeholder顯示與不顯示。這種方法對UITextView本身影響較小。學(xué)習(xí)自Fly_Elephant:《UITextView實現(xiàn)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字符串的長度,當(dāng)textView輸入內(nèi)容時,Placeholder 字符串的長度為nil,當(dāng)textView不輸入內(nèi)容時,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;// 調(diào)用通知的方法,讓placeholder顯示在UI上面[self textViewDidEndEditing:nil]; } #pragma mark - 重寫父類的text方法 - (NSString *)text {if ([super.text isEqualToString:self.placeholder]) {super.text = @"";}return super.text; } @end

  

轉(zhuǎn)載于:https://www.cnblogs.com/peaker-wu/p/5486176.html

總結(jié)

以上是生活随笔為你收集整理的UITextView实现PlaceHolder的方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。