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

头文件:

//
// FEPlaceHolderTextView.h
// MyTextViewDemo
//http://www.cnblogs.com/xiaofeixiang
// Created by keso on 15/5/17.
// Copyright (c) 2015年 keso. All rights reserved.
// #import <UIKit/UIKit.h> @interface FEPlaceHolderTextView : UITextView @property (nonatomic, retain) NSString *placeholder;
@property (nonatomic, retain) UIColor *placeholderColor; -(void)textChanged:(NSNotification*)notification; @end

 实现文件:

//
// FEPlaceHolderTextView.m
// MyTextViewDemo
//http://www.cnblogs.com/xiaofeixiang
// Created by keso on 15/5/17.
// Copyright (c) 2015年 keso. All rights reserved.
// #import "FEPlaceHolderTextView.h" @interface FEPlaceHolderTextView () @property (nonatomic, retain) UILabel *placeHolderLabel; @end @implementation FEPlaceHolderTextView CGFloat 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.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

 调用:

    self.textView=[[FEPlaceHolderTextView alloc]initWithFrame:CGRectMake(10, 30, CGRectGetWidth(self.view.frame)-20, 200)];
self.textView.placeholder=@"博客园FlyElephant\n博客地址:http://www.cnblogs.com/xiaofeixiang";
self.textView.layer.borderColor=[UIColor lightGrayColor].CGColor;
self.textView.layer.borderWidth=1.0;
self.textView.scrollEnabled = YES;
self.textView.autoresizingMask =
UIViewAutoresizingFlexibleHeight; //自适应高度
self.textView.returnKeyType = UIReturnKeyDefault; //返回键的类型 self.textView.keyboardType = UIKeyboardTypeDefault; //键盘类型
[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. 卡在 构建 gradle 项目信息

    之所以卡死是因为Android Studio在初次构建的时候,会联网进行gradle目录的更新操作.断网之后就不会去主动更新了. 断网. 打开项目. 把 gradle 改成 使用当地. ======= ...

  2. Nginx增加模块

    http://blog.csdn.net/loyachen/article/details/50902667

  3. HDU 5842 Lweb and String 水题

    Lweb and String 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5842 Description Lweb has a string S ...

  4. mysql分页查询优化(索引延迟关联)

    对于web后台报表导出是一种常见的功能点,实际对应服务后端即数据库的排序分页查询.如下示例为公司商户积分报表导出其中一个sql ,当大批量的导出请求进入时候,mysql的cpu急剧上升瞬间有拖垮库的风 ...

  5. IntraWeb XIV 类型速查表

    tkClass ================== IWUserSessionBase.TIWUserSessionBase < TDataModule < TComponent < ...

  6. 关于 TRegEx.Split()

    表达式中的括号将严重影响分割结果. uses RegularExpressions; const FSourceText = '1: AAA 2: BBB 3: CCC'; // 分隔符将有三部分构成 ...

  7. java之jvm学习笔记十三(jvm基本结构) 通俗易懂的JVM 文件,没有之一

    http://blog.csdn.net/yfqnihao/article/details/8289363

  8. IE中div被视频遮住的解决方法

    使用embed来内嵌视频,因为视频是windows media player,上面想用div浮动一些内容,之前尝试了一些方法,比如 1. 通过设定不同组件的z-index值 2. 通过设定 wmode ...

  9. C#写的COM组件注册问题兼论微软Regasm注册的BUG

    工作中自己用C#写了专门读写EXCEL(不需要OFFICE环境,直接读原始文件,速度快)的COM组件,在使用过程中,发现原先的注册程序是有问题的.网上也有同样的网友碰到这个问题,但都没找到合适的解决办 ...

  10. finger-guessing game:3增加猜拳次数及猜拳按钮显示

    增加猜拳次数及猜拳按钮 //初始化引擎组件 init(50, "div_caiquan", 800, 400, main); //定义游戏层,加载进度层.游戏背景层,结果显示层,出 ...