设计带有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的更多相关文章

  1. 李洪强iOS开发之带placeHolder的Textview

    李洪强iOS开发之带placeHolder的Textview  01 - 创建工过程,定义全局属性,遵守textview的代理协议  02 - 添加一个textview和一个label 03 - 实现 ...

  2. 新浪微博客户端(36)-自定义带placeholder的TextView

    iOS 上自带的UITextView竟然不能设置placeholder,但是UITextView却可以,我也真是醉了.没办法了,自己写一个 DJTextView.h #import <UIKit ...

  3. [web设计]带有方向感应的hover effect

    See the Pen bdxLQa by jeremylee (@lijie33402) on CodePen. codepen不知道怎么嵌入到cnblogs..待编辑 参考资料 参考博客

  4. iOS 第三方库、插件、知名博客总结

    iOS 第三方库.插件.知名博客总结 用到的组件 1.通过CocoaPods安装 项目名称 项目信息 AFNetworking 网络请求组件 FMDB 本地数据库组件 SDWebImage 多个缩略图 ...

  5. iOS -- 开源项目和库

    TimLiu-iOS 目录 UI 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD与Toast 对话框 其他UI 动画 侧滑与右滑返回手势 gif动画 ...

  6. ios很好的开源库

    Tim9Liu9/TimLiu-iOS 自己总结的iOS.mac开源项目及库,持续更新.. 目录 UI 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD ...

  7. [iOS UI进阶 - 1] 自定义控件

    A.关于Quiartz2D的一些细节 1.UIKit的工具已经封装了上下文引用,所以不用手动获取和渲染 - (void)drawRect:(CGRect)rect { [[UIColor redCol ...

  8. iOS TextView输入长度限制 设置placeholder

    textView在使用中通常会有2个功能是最常用的 设置placeholder 限制输入长度 TYLimitedTextView刚好是为了解决这个2个问题而诞生的,下面讲解TYLimitedTextV ...

  9. iOS开发-带Placeholder的UITextView实现

    iOS中UITextField带有PlaceHolder属性,可以方便用于提示输入.但是同样可以进行文本输入的UITextView控件则没有PlaceHolder属性,还是有些不方便的,尤其是对于略带 ...

随机推荐

  1. 恶性bug解决,Encoding 1252 data could not be found. Make sure you have correct international codeset assembly installed and enabled

    百度是没有的,google了下 这句话的意思是编码1252没找到,确保程序及是国际化格式 发生在我使用unity读取xlsx文件,在编辑器运行正常,但是发布出来不正常,报错 解决方案: 链接:http ...

  2. 自然语言处理--TF-IDF(关键词提取)

    TF-IDF算法 TF-IDF(词频-逆文档频率)算法是一种统计方法,用以评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度.字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它 ...

  3. Keepalived配置与使用--转载

    作者: JeremyWei | 可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明 网址: http://weizhifeng.net/using-keepalived.html 介绍 ...

  4. CUBA China 最新进展

    各位关注CUBA平台的朋友,你们好! 距上次发布动态我们又沉默了大概两个月时间,这期间我们一直在翻译CUBA平台的文档.CUBA平台的开发文档相当丰富,所以这需要耗费较多的时间,至少比我们预想的时间要 ...

  5. MongoDB之数据库管理

    前面补充过把MongoDB作为服务,今天主要了解数据库管理,在操作之前先把MongoDB服务打开. 一.显示数据库清单 如果想查看数据库列表可以使用show dbs. 二.切换数据库 通过使用Mong ...

  6. [转载]Jquery mobiscroll 移动设备(手机)wap日期时间选择插件以及滑动、滚动插件

    Jquery Mobiscroll是一个用于触摸设备(Android phones, iPhone, iPad, Galaxy Tab)的日期和时间选择器jQuery插件.以及各种滑动插件 可以让用户 ...

  7. BOM-使用定时器

    window对象包含4个定时器专用方法,说明如下表所示,使用它们可以实现代码定时运行,避免连续执行,这样可以设计动画 方法 说明 setInterval() 按照指定的周期,(以毫秒为单位)来调用函数 ...

  8. [转] 疯狂的JSONP

      何为跨域?何为JSONP?JSONP技术能实现什么?是否有必要使用JSONP技术? 跨域 就是由于JavaScript同源策略的限制,使得a.com域名下的js无法操作b.com或c.a.com域 ...

  9. Android Studio 3.0找不到Android Device Monitor

    因为自Android Studio 3.0开始弃用Android Device Monitor,Android Developers官网上的原话是: Android Device Monitor is ...

  10. .net core 2.2 部署CentOS7(4)CentOS7下载并安装.NET SDK(软件开发工具包)

    目录: .net core 2.2 部署CentOS7(1)安装虚拟机 .net core 2.2 部署CentOS7(2)给虚拟机安装CentOS7 .net core 2.2 部署CentOS7( ...