iOS开发-UITextView实现PlaceHolder的方式
之前开发遇到过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的方式的更多相关文章
- 【Xamarin 挖墙脚系列:IOS 开发界面的3种方式】
原文:[Xamarin 挖墙脚系列:IOS 开发界面的3种方式] xcode6进行三种基本的界面布局的方法,分别是手写UI,xib和storyboard.手写UI是最早进行UI界面布局的方法,优点是灵 ...
- UITextView实现PlaceHolder的方式
实现UITextView实现PlaceHolder的方式的方式有两种,这两种方法的核心就是通过通知来添加和去除PlaceHolder:下面来介绍两种方法:个人比较喜欢第一种,看起来更加合理. 方法1: ...
- 怎样实现IOS开发中的数据存储方式
iOS 开发中,一般有如下几种数据存储方式.需要根据具体的业务场景,选择 合适的数据存储方式. (1) 用户默认设置 – 这种情况通常不需要用户干预,如游戏通关信息,Video 播放记录,或者 Ap ...
- iOS开发 - CocoaPods的常见使用方式
1 CocoaPods 的安装 1.1 作用: 帮助管理和维护第三方框架,快速的搜索到第三方框架, 然后自动集成到工程里面来, 并编译成一个libPod.a的静态库给我们项目用 1.2 理解: 1. ...
- iOS开发系列-常见离线存储方式
概述 在很多社交App手机在手机没有网络时,重新启动应用,依然能否展示上次访问的数据,提高用户体验,这个就是离线数据存储的运用场景.在iOS开发中常见的离线存储技术有Plist存储.个人偏好存储.解归 ...
- ios开发使用Basic Auth 认证方式
http://blog.csdn.net/joonchen111/article/details/48447813 我们app的开发通常有2种认证方式 一种是Basic Auth,一种是OAuth ...
- iOS开发-UITextView根据内容自适应高度
UITextView作为内容文本输入区域,有的时候我们需要根据内容动态改变文本区域的高度,效果如下: 定义UITextView,实现UITextViewDelegate: -(UITextView * ...
- 【转】ios开发之生成所缩略图方式
亲测:两种方式都有效 第一种方式:缩略成固定的尺寸大小 - (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSiz ...
- 【iOS开发-79】利用Modal方式实现控制器之间的跳转
利用Modal方法.事实上就是以下两个方法的运用. Modal方式的切换效果是从底部呈现. -(void)clickModal{ WPViewController *wp=[[WPViewContro ...
随机推荐
- iOS 11开发教程(十四)iOS11应用代码添加视图
iOS 11开发教程(十四)iOS11应用代码添加视图 如果开发者想要使用代码为主视图添加视图,该怎么办呢.以下将为开发者解决这一问题.要使用代码为主视图添加视图需要实现3个步骤. (1)实例化视图对 ...
- Java中实现多线程的两种方式之间的区别
Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线 ...
- 20172308《Java软件结构与数据结构》第二周学习总结
教材学习内容总结 第 3 章 集合概述--栈 集合:一种聚集.组织了其他对象的对象 软件系统中的另一个类或对象通过集合预定的方式与该集合进行交互来使用这些集合 多年以来软件开发和研究人员定义了一些特定 ...
- Mac 10.13安装telnet
狗日的Mac 10.13默认不自带telnet!!!苹果你以为你的操作系统真的那么平民吗,别做梦,用你只不过是为了开发!!! 安装: brew install telnet 如果你用上述方法安装不上, ...
- STM32 F4 Clock Sources
STM32 F4 Clock Sources Goal: routing clock sources to the microcontroller output pin (MCO1) High- ...
- STM32 Controller area network (bxCAN) Identifier filtering
Identifier filtering In the CAN protocol the identifier of a message is not associated with the addr ...
- 【Go入门教程2】基本构成元素:标识符(identifier)、关键字(keyword 25个)、字面量(literal)、分隔符(delimiter)、和 操作符(operator)
基本构成要素 Go 的语言符号 又称 词法元素,共包括 5 类内容——标识符(identifier).关键字(keyword).字面量(literal).分隔符(delimiter) 和 操作符(op ...
- RabbitMQ的应用场景以及基本原理介绍(转)
本文转自https://blog.csdn.net/whoamiyang/article/details/54954780 1.背景 RabbitMQ是一个由erlang开发的AMQP(Advanve ...
- sqlite 截取字符串函数substr
函数:substr(string string,num start,num length) 用法: string为字符串: start为起始位置:字符串的第一个字符的位置为1,不是从0开始计算 len ...
- DotNetty 学习
[转载]http://www.cnblogs.com/littlegod/p/7699482.html DotNetty的学习是带着如下这些问题展开: 1. Socket基础框架方案: 通信模式:异步 ...