iOS开发-UITextView实现PlaceHolder的方式

十度 IOS 2015年12月01日 收藏

之前开发遇到过UITextField中加入一个PlaceHolder的问题,直接设置一下即可,不过这次是需要在UITextView中实现一个PlaceHolder,跟之前有点不同。在网上参考了各位前辈的解决方案,大概有两种方式,第一种方式很猥琐,就是直接给UITextView的text赋值,比如说默认提示是"博客园FlyElephant",在textViewDidChange中判断是不是“博客园FlyElephant”,如果是就清空,如果不是就继续提示,弊端就是用户输入的内容不能和你的默认提示一样,第二种方式需要加入一个UILabel,同样在textViewDidChange中进行判断,一般都是这么实现,不过有的都是创建textView中的时候创建UILabel,这样做无可厚非,不过最好还是抽象出来。继承UITextView扩展一下,新建一个FEPlaceHolderTextView:

头文件:

  1. //
  2. // FEPlaceHolderTextView.h
  3. // MyTextViewDemo
  4. //http://www.cnblogs.com/xiaofeixiang
  5. // Created by keso on 15/5/17.
  6. // Copyright (c) 2015年 keso. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface FEPlaceHolderTextView : UITextView
  12.  
  13. @property (nonatomic, retain) NSString *placeholder;
  14. @property (nonatomic, retain) UIColor *placeholderColor;
  15.  
  16. -(void)textChanged:(NSNotification*)notification;
  17.  
  18.  
  19. @end

 实现文件:

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

 调用:

  1. self.textView=[[FEPlaceHolderTextView alloc]initWithFrame:CGRectMake(10, 30, CGRectGetWidth(self.view.frame)-20, 200)];
  2. self.textView.placeholder=@"博客园FlyElephant\n博客地址:http://www.cnblogs.com/xiaofeixiang";
  3. self.textView.layer.borderColor=[UIColor lightGrayColor].CGColor;
  4. self.textView.layer.borderWidth=1.0;
  5. self.textView.scrollEnabled = YES;
  6. self.textView.autoresizingMask =
  7. UIViewAutoresizingFlexibleHeight; //自适应高度
  8. self.textView.returnKeyType = UIReturnKeyDefault; //返回键的类型
  9. self.textView.keyboardType = UIKeyboardTypeDefault; //键盘类型
  10. [self.view addSubview:self.textView];

效果: