用UITextView模拟UITextField的placeHolder

效果:

源码:

//
// ViewController.m
// TextView
//
// Created by YouXianMing on 14/12/18.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h" static NSString *placeHolderStr = @"User Name"; @interface ViewController ()<UITextViewDelegate> @property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIButton *button; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 反应的按钮
self.button = [[UIButton alloc] initWithFrame:self.view.bounds];
[self.button addTarget:self
action:@selector(buttonEvent)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button]; // textView
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(, , , )];
self.textView.layer.borderWidth = .f;
self.textView.layer.borderColor = [UIColor grayColor].CGColor;
self.textView.delegate = self;
self.textView.text = placeHolderStr;
self.textView.font = [UIFont systemFontOfSize:.f];
self.textView.textColor = [UIColor grayColor];
[self.view addSubview:self.textView];
} #pragma mark - 代理方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
// 设置编辑状态文字颜色
textView.textColor = [UIColor blackColor]; // 如果文字为placeHolder文字
if ([textView.text isEqualToString:placeHolderStr]) {
textView.text = @"";
} return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView { // 如果长度为0,则显示placeHolder文字
if (textView.text.length == ) {
textView.text = placeHolderStr;
textView.textColor = [UIColor grayColor];
} return YES;
} /**
* 反应的按钮
*/
- (void)buttonEvent {
[self.textView resignFirstResponder];
} @end

核心代码:

用UITextView模拟UITextField的placeHolder的更多相关文章

  1. UITextView模拟UITextField 设置Placeholder属性 --董鑫

    由于最近有用到输入框,刚开始考虑的是UITextField,因为它在没有输入的时候可以有提示的Placeholder更能,很人性化,但UITextField只能单行输入,不能跳行,对于一些强迫症的亲来 ...

  2. UITextView和UITextField的placeholder,键盘隐藏,键盘换行变完成字样

    本文转载至 http://blog.csdn.net/hengshujiyi/article/details/9086093- (void)initFeedBackViews { //设置页面的背景颜 ...

  3. [BS-19]更改UITextField的placeholder文字颜色的5种方法

    更改UITextField的placeholder文字颜色的5种方法 想要达到的目标是:一个页面上有多个UITextField,当用户聚焦某textField时,该文本框的placeholder的文字 ...

  4. 修改UITextfield的Placeholder字体的颜色

    - (void)viewDidLoad { [super viewDidLoad]; self.title=@"修改UITextField的placeholder字体颜色"; UI ...

  5. UITextView与UITextfield的区别

    IOS中的UITextView和UITextField都是文本输入控件并都能够调用系统键盘.本次特酷把介绍UITextView和UITextField的区别.简单来说,UITextView和UITex ...

  6. iOS UITextField设置placeholder颜色

    设置UITextField的placeholder颜色 UIColor *color = [UIColor blackColor]; textField.attributedPlaceholder = ...

  7. 【修改 UITextField 中 placeholder 的顏色】

    第一种方法: [textfeild setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; ...

  8. UITextfield设置Placeholder颜色 控件 内边距、自适应高度

    //创建UITextField对象 UITextField * tf=[[UITextField alloc]init];    //设置Placeholder颜色 [text setAttribut ...

  9. iOS-改变UITextField的Placeholder颜色的三种方式

    转自:http://blog.csdn.net/mazy_ma/article/details/51775670 有时,UITextField自带的Placeholder的颜色太浅或者不满足需求,所以 ...

随机推荐

  1. Chapter 3 Phenomenon——2

    I had enough trouble not falling down when the ground was dry; it might be safer for me to go back t ...

  2. Spring Boot 的彩色日志

    springboot的彩色日志灰常漂亮, 看起来也很舒服. 但是自定义的日志就是一纯白色的, 丑到不行. 所以就copy他的彩色日志来养眼: <!-- 彩色日志 --> <!-- 彩 ...

  3. Go 开发

    0.参数传递永远是值传递,地址也是一种值 1.go 开发环境的配置 2.import 包的几种形式: 1)_,默认导入一个包时,会将包中内容导入再执行包中的init()方法,有时并不需要某个包,只是想 ...

  4. PHP面向对象常见符号总结($this-> 、self ::)

    转载:http://wyllife.blog.163.com/blog/static/4116390120116223528180/ 在php中常见的对象符号 1.$this this是指向当前对象的 ...

  5. Java并发编程笔记之ThreadLocalRandom源码分析

    JDK 并发包中 ThreadLocalRandom 类原理剖析,经常使用的随机数生成器 Random 类的原理是什么?及其局限性是什么?ThreadLocalRandom 是如何利用 ThreadL ...

  6. Maven可以提交到官方公共仓库maven.org

    参考http://central.sonatype.org/pages/ossrh-guide.html这个网址的操作,即可提交到maven.org. 这里有具体的实践方法:http://blog.c ...

  7. ARM的体系结构与编程系列博客——ARM的历史与应用范围

    前言 最近我感觉自己比较浮躁,重来没有好好地沉下心来做一件事情,而且针对自己在专业水平上仍然还有很多欠缺,于是我想我应该为自己做些什么来证明一下自己真的是潜心研究东西的人,于是我萌生了一个想法,真正地 ...

  8. Android源码博客目录

    每次都找不到,干脆每个部分都开个目录,方便找 0. 杂项 一些Android的博客,没事翻翻 1. 构建相关 linux和Android的Makefile和android.mk android 目录下 ...

  9. 【JavaScript 从零开始】表达式和运算符(1)

    原始表达式 最简单的表达式是"原始表达式"(primary expression).JavaScript中的原始表达式包含常量或直接量.关键字和变量. // 常量或直接量 1.23 ...

  10. SQL查询几种的区别。

    最近看了几篇SQL查询的文章做一下总结哦,大概简记如下: SQL查询的实质是,是指从数据库中取得数据的子集,可以先取列子集,然后再取符合条件的行子集. 1.单表查询: SELECT [Name] ,[ ...