设计带有placeHolder的TextView
设计带有placeHolder的TextView

效果:

源码:
PlaceholderTextView.h 与 PlaceholderTextView.m
//
// PlaceholderTextView.h
// YXTextView
//
// Created by YouXianMing on 14/12/23.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface PlaceholderTextView : UIView // 获取的字符串
@property (nonatomic, strong, readonly) NSString *string; // textView
@property (nonatomic, strong) UITextView *textView; // 占位字符
@property (nonatomic, strong) NSString *placeHolderString; // 文本边缘留白
@property(nonatomic, assign) UIEdgeInsets textContainerInset; // 颜色设置
@property (nonatomic, strong) UIColor *editTextColor;
@property (nonatomic, strong) UIColor *placeHolderColor; // 返回键是否用来做取消第一响应者
@property (nonatomic, assign) BOOL returnButtonToResignFirstResponder; // 取消第一响应者
- (void)resignTextViewFirstResponder; @end
//
// PlaceholderTextView.m
// YXTextView
//
// Created by YouXianMing on 14/12/23.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "PlaceholderTextView.h" @interface PlaceholderTextView ()<UITextViewDelegate> @property (nonatomic, strong) NSString *string; @end @implementation PlaceholderTextView - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createTextView];
}
return self;
} - (void)createTextView {
self.textView = [[UITextView alloc] initWithFrame:self.bounds];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor clearColor];
self.textView.textColor = [UIColor grayColor];
[self addSubview:self.textView];
} #pragma mark - 代理方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
// 设置编辑状态文字颜色
textView.textColor = (self.editTextColor == nil ? [UIColor blackColor] : self.editTextColor); // 如果文字为placeHolder文字
if ([textView.text isEqualToString:self.placeHolderString]) {
textView.text = @"";
} return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
// 如果长度为0,则显示placeHolder文字
if (textView.text.length == ) {
textView.text = self.placeHolderString;
textView.textColor = (self.placeHolderColor == nil ? [UIColor grayColor] : self.placeHolderColor);
} return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if (_returnButtonToResignFirstResponder == YES) {
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
} return YES;
} - (void)resignTextViewFirstResponder {
[self.textView resignFirstResponder];
} #pragma mark - 重写setter,getter方法
@synthesize string = _string;
- (NSString *)string {
if ([self.textView.text isEqualToString:self.placeHolderString]) {
return @"";
} else {
return self.textView.text;
}
}
@synthesize placeHolderColor = _placeHolderColor;
- (void)setPlaceHolderColor:(UIColor *)placeHolderColor {
_placeHolderColor = placeHolderColor;
self.textView.textColor = _placeHolderColor;
}
- (UIColor *)placeHolderColor {
return _placeHolderColor;
} @synthesize placeHolderString = _placeHolderString;
- (void)setPlaceHolderString:(NSString *)placeHolderString {
_placeHolderString = placeHolderString;
_textView.text = placeHolderString;
}
- (NSString *)placeHolderString {
return _placeHolderString;
}
@synthesize textContainerInset = _textContainerInset;
- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset {
_textContainerInset = textContainerInset;
_textView.textContainerInset = textContainerInset;
}
- (UIEdgeInsets)textContainerInset {
return _textContainerInset;
} @end
控制器源码:
//
// ViewController.m
// YXTextView
//
// Created by YouXianMing on 14/12/23.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "PlaceholderTextView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; PlaceholderTextView *textView = [[PlaceholderTextView alloc] initWithFrame:CGRectMake(-, , , )];
[self.view addSubview:textView]; textView.layer.borderWidth = .f;
textView.layer.borderColor = [UIColor purpleColor].CGColor; textView.returnButtonToResignFirstResponder = YES;
textView.textView.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
textView.placeHolderColor = [UIColor cyanColor];
textView.editTextColor = [UIColor redColor];
textView.textContainerInset = UIEdgeInsetsMake(, , , );
textView.placeHolderString = @"Input your name, please...";
} @end
需要注意的一些细节:

设计带有placeHolder的TextView的更多相关文章
- 李洪强iOS开发之带placeHolder的Textview
李洪强iOS开发之带placeHolder的Textview 01 - 创建工过程,定义全局属性,遵守textview的代理协议 02 - 添加一个textview和一个label 03 - 实现 ...
- 新浪微博客户端(36)-自定义带placeholder的TextView
iOS 上自带的UITextView竟然不能设置placeholder,但是UITextView却可以,我也真是醉了.没办法了,自己写一个 DJTextView.h #import <UIKit ...
- [web设计]带有方向感应的hover effect
See the Pen bdxLQa by jeremylee (@lijie33402) on CodePen. codepen不知道怎么嵌入到cnblogs..待编辑 参考资料 参考博客
- iOS 第三方库、插件、知名博客总结
iOS 第三方库.插件.知名博客总结 用到的组件 1.通过CocoaPods安装 项目名称 项目信息 AFNetworking 网络请求组件 FMDB 本地数据库组件 SDWebImage 多个缩略图 ...
- iOS -- 开源项目和库
TimLiu-iOS 目录 UI 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD与Toast 对话框 其他UI 动画 侧滑与右滑返回手势 gif动画 ...
- ios很好的开源库
Tim9Liu9/TimLiu-iOS 自己总结的iOS.mac开源项目及库,持续更新.. 目录 UI 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD ...
- [iOS UI进阶 - 1] 自定义控件
A.关于Quiartz2D的一些细节 1.UIKit的工具已经封装了上下文引用,所以不用手动获取和渲染 - (void)drawRect:(CGRect)rect { [[UIColor redCol ...
- iOS TextView输入长度限制 设置placeholder
textView在使用中通常会有2个功能是最常用的 设置placeholder 限制输入长度 TYLimitedTextView刚好是为了解决这个2个问题而诞生的,下面讲解TYLimitedTextV ...
- iOS开发-带Placeholder的UITextView实现
iOS中UITextField带有PlaceHolder属性,可以方便用于提示输入.但是同样可以进行文本输入的UITextView控件则没有PlaceHolder属性,还是有些不方便的,尤其是对于略带 ...
随机推荐
- 探秘varian:优雅的发布部署程序
上一篇文章<记一次诡异的故障排查经历>中有介绍到我们的部署程序varian,文章发布后有小伙伴对varian很感兴趣,今天就简单的介绍一下我们的varian,揭开她神秘的面纱~ 什么是va ...
- InterView之PHP(2)
PHP 理论知识 常用的超全局变量(8个) $_GET ----->get传送方式 $_POST ----->post传送方式 $_REQUEST ----->可以接收到get和po ...
- C语言——<计算>_较大两个数相乘
例题:9876543210*1234567890 的乘积 分析:正常的数据结构已经无法满足这么大的数相乘的结果.只能使用数组来进行操作. 1.两个数都用字符数组来接收. 2.接收后,因为每一位要乘以另 ...
- cordova打包APK,SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode ...
javascript 严格模式 第一次接触let关键字,有一个要非常非常要注意的概念就是”javascript 严格模式”,比如下述的代码运行就会报错: let hello = 'hello worl ...
- Shell脚本编写4-----Shell 流程控制
没啥好说的,直接从demo里看吧!(1) if 语句shell脚本的if语句格式如下: 判断输入两个参数的大小,执行结果如下 (2)for 循环for循环语法格式如下: 执行结果如下 (3)while ...
- [转]React 教程
本文转自:http://www.runoob.com/react/react-install.html React 可以直接下载使用,下载包中也提供了很多学习的实例. 本教程使用了 React 的版本 ...
- Oracle安装后遇到错误:The Network Adapter could not establish the connection
http://note.youdao.com/noteshare?id=e6baee7ea7b7f60d7a265124e2bdd46c&sub=988945C6DDE843D5A7D6588 ...
- maven配置Mac平台
Mac OS X 安装Maven: 下载 Maven, 并解压到某个目录.例如/Users/robbie/apache-maven-3.3.3 打开Terminal,输入以下命令,设置Maven cl ...
- 基于JSP的RSS阅读器的设计与实现
阅读器访问地址:http://easyrss.tk/,欢迎体验! 阅读导览 一. 概述 二. 设计的基本概念和原理 三. 设计方案 四. 主要源代码 五. 阅读器使用说 ...
- JVM之---Java源码编译机制
Sun JDK中采用javac将Java源码编译为class文件,这个过程包含三个步骤: 1.分析和输入到符号表(Parse and Enter) Parse过程所做的工作有词法和语法分 ...