实现UITextView实现PlaceHolder的方式的方式有两种,这两种方法的核心就是通过通知来添加和去除PlaceHolder;下面来介绍两种方法;个人比较喜欢第一种,看起来更加合理。

方法1:原理是通过通知来改变PlaceHolder,把PlaceHolder看成是一个UILabel,设置UILabel的透明度,来让Placeholder显示与不显示。这种方法对UITextView本身影响较小。学习自Fly_Elephant《UITextView实现PlaceHolder的方式》这篇文章

.h文件

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface DLTextView : UITextView
  4.  
  5. @property (nonatomic, retain) NSString *placeholder;
  6.  
  7. @property (nonatomic, retain) UIColor *placeholderColor;
  8.  
  9. - (void)textChanged:(NSNotification*)notification;
  10.  
  11. @end

.m文件

  1. #import "DLTextView.h"
  2.  
  3. @interface DLTextView ()
  4. @property (nonatomic, retain) UILabel *placeHolderLabel;
  5.  
  6. @end
  7.  
  8. @implementation DLTextView
  9.  
  10. CGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25;
  11.  
  12. - (void)dealloc
  13. {
  14. [[NSNotificationCenter defaultCenter] removeObserver:self];
  15. }
  16.  
  17. - (void)awakeFromNib
  18. {
  19. [super awakeFromNib];
  20. if (!self.placeholder) {
  21. [self setPlaceholder:@""];
  22. }
  23.  
  24. if (!self.placeholderColor) {
  25. [self setPlaceholderColor:[UIColor lightGrayColor]];
  26. }
  27.  
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
  29. }
  30.  
  31. - (id)initWithFrame:(CGRect)frame
  32. {
  33. if( (self = [super initWithFrame:frame]) )
  34. {
  35. [self setPlaceholder:@""];
  36. [self setPlaceholderColor:[UIColor lightGrayColor]];
  37. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
  38. }
  39. return self;
  40. }
  41.  
  42. - (void)textChanged:(NSNotification *)notification
  43. {
  44. if([[self placeholder] length] == 0)
  45. {
  46. return;
  47. }
  48.  
  49. [UIView animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION animations:^{
  50. if([[self text] length] == 0)
  51. {
  52. [[self viewWithTag:999] setAlpha:1];
  53. }
  54. else
  55. {
  56. [[self viewWithTag:999] setAlpha:0];
  57. }
  58. }];
  59. }
  60.  
  61. - (void)setText:(NSString *)text {
  62. [super setText:text];
  63. [self textChanged:nil];
  64. }
  65.  
  66. - (void)drawRect:(CGRect)rect
  67. {
  68. if( [[self placeholder] length] > 0 )
  69. {
  70. if (_placeHolderLabel == nil ) {
  71.  
  72. _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, self.bounds.size.width, 10)];
  73. _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
  74. _placeHolderLabel.numberOfLines = 0;
  75. // _placeHolderLabel.font = self.font;
  76. _placeHolderLabel.font = [UIFont systemFontOfSize:13.0];
  77. _placeHolderLabel.backgroundColor = [UIColor clearColor];
  78. _placeHolderLabel.textColor = self.placeholderColor;
  79. _placeHolderLabel.alpha = 0;
  80. _placeHolderLabel.tag = 999;
  81.  
  82. [self addSubview:_placeHolderLabel];
  83. }
  84.  
  85. _placeHolderLabel.text = self.placeholder;
  86. [_placeHolderLabel sizeToFit];
  87. [self sendSubviewToBack:_placeHolderLabel];
  88. }
  89.  
  90. if( [[self text] length] == 0 && [[self placeholder] length] > 0 )
  91. {
  92. [[self viewWithTag:999] setAlpha:1];
  93. }
  94.  
  95. [super drawRect:rect];
  96. }
  97.  
  98. @end

 第二种方法:原理是通过Placeholder字符串的长度,当textView输入内容时,Placeholder 字符串的长度为nil,当textView不输入内容时,Placeholder显示。

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface DLTextView : UITextView
  4.  
  5. @property (nonatomic, copy) NSString *placeholder;
  6.  
  7. @end

  

  1. #import "DLTextView.h"
  2.  
  3. @implementation DLTextView
  4.  
  5. - (instancetype)initWithFrame:(CGRect)frame
  6. {
  7. self = [super initWithFrame:frame];
  8. if (self) {
  9. [self setup];
  10. }
  11. return self;
  12. }
  13. - (instancetype)initWithCoder:(NSCoder *)decoder
  14. {
  15. self = [super initWithCoder:decoder];
  16. if (self) {
  17. [self setup];
  18.  
  19. }
  20. return self;
  21. }
  22. - (void)awakeFromNib
  23. {
  24. // [self setup];
  25.  
  26. }
  27.  
  28. - (void)setup
  29. {
  30. // NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:super.text];
  31. // NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  32. // paragraphStyle.lineSpacing = 10;
  33. //
  34. // [attributedText addAttributes:@{NSParagraphStyleAttributeName : paragraphStyle} range:NSMakeRange(0, super.text.length)];
  35. //
  36. // super.attributedText = attributedText;
  37.  
  38. // 添加通知
  39. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil];
  41. }
  42. - (void)dealloc
  43. {
  44. [[NSNotificationCenter defaultCenter] removeObserver:self];
  45. }
  46. #pragma mark - 开始编辑的消息方法
  47. - (void)textViewDidBeginEditing:(NSNotification *)sender
  48. {
  49. // 开始编辑的时候,父控件的text如果等于placeholder就让什么也不显示
  50. if ([super.text isEqualToString:self.placeholder]) {
  51. super.text = @"";
  52. [super setTextColor:[UIColor blackColor]];
  53. }
  54.  
  55. }
  56.  
  57. - (void)textViewDidEndEditing:(NSNotification *)sender
  58. {
  59. if (super.text.length == 0) {
  60. super.text = self.placeholder;
  61. [super setTextColor:[UIColor grayColor]];
  62. }
  63. }
  64. - (void)setPlaceholder:(NSString *)placeholder
  65. {
  66. _placeholder = placeholder;
  67.  
  68. // 调用通知的方法,让placeholder显示在UI上面
  69. [self textViewDidEndEditing:nil];
  70. }
  71. #pragma mark - 重写父类的text方法
  72. - (NSString *)text
  73. {
  74. if ([super.text isEqualToString:self.placeholder]) {
  75.  
  76. super.text = @"";
  77. }
  78.  
  79. return super.text;
  80. }
  81. @end

  

UITextView实现PlaceHolder的方式的更多相关文章

  1. iOS开发-UITextView实现PlaceHolder的方式

    之前开发遇到过UITextField中加入一个PlaceHolder的问题,直接设置一下即可,不过这次是需要在UITextView中实现一个PlaceHolder,跟之前有点不同.在网上参考了各位前辈 ...

  2. UITextView实现placeHolder方法汇总

    UITextField中有一个placeholder属性,可以设置UITextField的占位文字,起到提示用户的作用.可是UITextView就没那么幸运了,apple没有给UITextView提供 ...

  3. 教大家怎样给UITextView加入placeholder扩展

    怎样扩展UITextView以追加placeholder功能呢? 我们的需求是:追加placeholder功能 方案讨论: 通过继承UITextView的方式 通过扩展UITextView的方式 分析 ...

  4. iOS - UITextView实现placeHolder占位文字

      iOS之UITextView实现placeHolder占位文字的N种方法 前言 iOS开发中,UITextField和UITextView是最常用的文本接受类和文本展示类的控件.UITextFie ...

  5. UITextView 实现placeholder的方法

    本文转载至 http://www.cnblogs.com/easonoutlook/archive/2012/12/28/2837665.html 在UITextField中自带placeholder ...

  6. 实现UITextView的placeholder

    我们知道在iOS开发时,控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UI ...

  7. UITextView设置placeholder

    下面是我的代码,可以直接拿来用 #import <UIKit/UIKit.h> @interface CustomTextView : UITextView @property(nonat ...

  8. spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化

    这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are p ...

  9. UITextView 实现placeholder

    1.在创建textView的时候,赋值其文本属性 即 textView.text = @"内容": 2.在开始编辑的代理方法中进行如下操作 - (void)textViewDidB ...

随机推荐

  1. textField 基本属性

    _textField.frame = CGRectMake(0, 0, 200, 50); _textField.delegate = self; _textField.text = str; [_t ...

  2. centos 7 内存压测测试--memtester工具

    1.下载memteste工具 官方:http://pyropus.ca/software/memtester/ wget http://pyropus.ca/software/memtester/ol ...

  3. rabbitmq设置队列消息存活时间

    public static final int ALIVETIME = 600000; public static final String QUEUE = "hnyz_gs_queue&q ...

  4. UML-重构

    1.重构是什么? 重构是重写或重新构建已有代码的结构化和规律性方法,但不会改变已有代码的外在行为,而是采用一系列少量转换的步骤,并且每一步都结合了重新执行的测试.重构并不是全部推翻原有代码结构. 2. ...

  5. 程序Dog的大梦想

    一.我是程序狗 "怎么又是任宏啊,每天都起这么早,要命啊--" "人家任宏可是要成为学霸的男人,咱们这些凡夫俗子啊,理解不了这么伟大的理想--"----微电影& ...

  6. Ubuntu的软件安装管理---dpkg与apt-*详解

    摘要:软件厂商先在他们的系统上面编译好了我们用户所需要的软件,然后将这个编译好并可执行的软件直接发布给用户安装.不同的 Linux 发行版使用不同的打包系统,一般而言,大多数发行版分别属于两大包管理技 ...

  7. 正确返回Unicode码点大于0xFFFF的字符串长度

    如下: function codePointLength(text){ var result = text.match(/[\s\S]/gu); return result? result.lengt ...

  8. UML-如何画顺序图?

    1.生命线框图和生命线 生命线:可以为虚线(源于UML1),也可以是实线 2.消息 1).创始消息,实心圆开头2).同步消息,实心箭头 3.执行规格条和控制期 控制期:阻塞调用 4.返回值 5.自身消 ...

  9. 蓝屏(BSOD)转储设置,看本文就够了!

    原总结注册表debug调试dump转储文件蓝屏BSODprocess monitor 前言 我们在 内核转储,开抓啦! 这篇文章里介绍了一个关键的系统设置.设置好后可以让系统在蓝屏(Blue Scre ...

  10. iOS 添加view的分类(更加方便的设置view的位置)

    点击创建UIView的分类category,这里命名为 PLExtension(为了和下面对应) view分类.h文件 #import <UIKit/UIKit.h> @interface ...