之前开发遇到过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. @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.  
  33. if (!self.placeholderColor) {
  34. [self setPlaceholderColor:[UIColor lightGrayColor]];
  35. }
  36.  
  37. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
  38. }
  39.  
  40. - (id)initWithFrame:(CGRect)frame
  41. {
  42. if( (self = [super initWithFrame:frame]) )
  43. {
  44. [self setPlaceholder:@""];
  45. [self setPlaceholderColor:[UIColor lightGrayColor]];
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
  47. }
  48. return self;
  49. }
  50.  
  51. - (void)textChanged:(NSNotification *)notification
  52. {
  53. if([[self placeholder] length] == 0)
  54. {
  55. return;
  56. }
  57.  
  58. [UIView animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION animations:^{
  59. if([[self text] length] == 0)
  60. {
  61. [[self viewWithTag:999] setAlpha:1];
  62. }
  63. else
  64. {
  65. [[self viewWithTag:999] setAlpha:0];
  66. }
  67. }];
  68. }
  69.  
  70. - (void)setText:(NSString *)text {
  71. [super setText:text];
  72. [self textChanged:nil];
  73. }
  74.  
  75. - (void)drawRect:(CGRect)rect
  76. {
  77. if( [[self placeholder] length] > 0 )
  78. {
  79. if (_placeHolderLabel == nil )
  80. {
  81. _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width,10)];
  82. _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
  83. _placeHolderLabel.numberOfLines =0;
  84. _placeHolderLabel.font = self.font;
  85. _placeHolderLabel.backgroundColor = [UIColor clearColor];
  86. _placeHolderLabel.textColor = self.placeholderColor;
  87. _placeHolderLabel.alpha = 0;
  88. _placeHolderLabel.tag = 999;
  89. [self addSubview:_placeHolderLabel];
  90. }
  91.  
  92. _placeHolderLabel.text = self.placeholder;
  93. [_placeHolderLabel sizeToFit];
  94. [self sendSubviewToBack:_placeHolderLabel];
  95. }
  96.  
  97. if( [[self text] length] == 0 && [[self placeholder] length] > 0 )
  98. {
  99. [[self viewWithTag:999] setAlpha:1];
  100. }
  101.  
  102. [super drawRect:rect];
  103. }
  104.  
  105. @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.  
  10. self.textView.keyboardType = UIKeyboardTypeDefault; //键盘类型
  11. [self.view addSubview:self.textView];

效果:

iOS开发-UITextView实现PlaceHolder的方式的更多相关文章

  1. 【Xamarin 挖墙脚系列:IOS 开发界面的3种方式】

    原文:[Xamarin 挖墙脚系列:IOS 开发界面的3种方式] xcode6进行三种基本的界面布局的方法,分别是手写UI,xib和storyboard.手写UI是最早进行UI界面布局的方法,优点是灵 ...

  2. UITextView实现PlaceHolder的方式

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

  3. 怎样实现IOS开发中的数据存储方式

    iOS 开发中,一般有如下几种数据存储方式.需要根据具体的业务场景,选择 合适的数据存储方式. (1)  用户默认设置 – 这种情况通常不需要用户干预,如游戏通关信息,Video 播放记录,或者 Ap ...

  4. iOS开发 - CocoaPods的常见使用方式

    1 CocoaPods 的安装 1.1 作用: 帮助管理和维护第三方框架,快速的搜索到第三方框架, 然后自动集成到工程里面来, 并编译成一个libPod.a的静态库给我们项目用 1.2 理解:  1. ...

  5. iOS开发系列-常见离线存储方式

    概述 在很多社交App手机在手机没有网络时,重新启动应用,依然能否展示上次访问的数据,提高用户体验,这个就是离线数据存储的运用场景.在iOS开发中常见的离线存储技术有Plist存储.个人偏好存储.解归 ...

  6. ios开发使用Basic Auth 认证方式

    http://blog.csdn.net/joonchen111/article/details/48447813 我们app的开发通常有2种认证方式   一种是Basic Auth,一种是OAuth ...

  7. iOS开发-UITextView根据内容自适应高度

    UITextView作为内容文本输入区域,有的时候我们需要根据内容动态改变文本区域的高度,效果如下: 定义UITextView,实现UITextViewDelegate: -(UITextView * ...

  8. 【转】ios开发之生成所缩略图方式

    亲测:两种方式都有效 第一种方式:缩略成固定的尺寸大小 - (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSiz ...

  9. 【iOS开发-79】利用Modal方式实现控制器之间的跳转

    利用Modal方法.事实上就是以下两个方法的运用. Modal方式的切换效果是从底部呈现. -(void)clickModal{ WPViewController *wp=[[WPViewContro ...

随机推荐

  1. iOS 11开发教程(十四)iOS11应用代码添加视图

    iOS 11开发教程(十四)iOS11应用代码添加视图 如果开发者想要使用代码为主视图添加视图,该怎么办呢.以下将为开发者解决这一问题.要使用代码为主视图添加视图需要实现3个步骤. (1)实例化视图对 ...

  2. Java中实现多线程的两种方式之间的区别

    Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线 ...

  3. 20172308《Java软件结构与数据结构》第二周学习总结

    教材学习内容总结 第 3 章 集合概述--栈 集合:一种聚集.组织了其他对象的对象 软件系统中的另一个类或对象通过集合预定的方式与该集合进行交互来使用这些集合 多年以来软件开发和研究人员定义了一些特定 ...

  4. Mac 10.13安装telnet

    狗日的Mac 10.13默认不自带telnet!!!苹果你以为你的操作系统真的那么平民吗,别做梦,用你只不过是为了开发!!! 安装: brew install telnet 如果你用上述方法安装不上, ...

  5. STM32 F4 Clock Sources

    STM32 F4 Clock Sources Goal: routing clock sources to the microcontroller output pin (MCO1)    High- ...

  6. STM32 Controller area network (bxCAN) Identifier filtering

    Identifier filtering In the CAN protocol the identifier of a message is not associated with the addr ...

  7. 【Go入门教程2】基本构成元素:标识符(identifier)、关键字(keyword 25个)、字面量(literal)、分隔符(delimiter)、和 操作符(operator)

    基本构成要素 Go 的语言符号 又称 词法元素,共包括 5 类内容——标识符(identifier).关键字(keyword).字面量(literal).分隔符(delimiter) 和 操作符(op ...

  8. RabbitMQ的应用场景以及基本原理介绍(转)

    本文转自https://blog.csdn.net/whoamiyang/article/details/54954780 1.背景 RabbitMQ是一个由erlang开发的AMQP(Advanve ...

  9. sqlite 截取字符串函数substr

    函数:substr(string string,num start,num length) 用法: string为字符串: start为起始位置:字符串的第一个字符的位置为1,不是从0开始计算 len ...

  10. DotNetty 学习

    [转载]http://www.cnblogs.com/littlegod/p/7699482.html DotNetty的学习是带着如下这些问题展开: 1. Socket基础框架方案: 通信模式:异步 ...