用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. flask小demo-数据查询

    mysqlconn-flask.py # -*- coding: utf-8 -*- #coding=utf-8 import os import mysql.connector from flask ...

  2. java的break,另一种用法(多层循环嵌套)

    break的另一种用法: 1.跳出外循环 outer:for(int j=0;i<4;j++){//outer随便定义的一个标签 for(int i=0;i<10;i++){ if(i== ...

  3. Linux发行版,分类,CentOS下载

    目录 Debian 系(dpkg) 1. 包管理 2. 发行版 Red Hat 系(rpm) 1. 包管理 2. 发行版 其他发行版   Debian 系(dpkg) 1. 包管理 包管理器:dpkg ...

  4. Pipenv——最好用的python虚拟环境和包管理工具

    pipenv 是Kenneth Reitz大神的作品,能够有效管理Python多个环境,各种包.过去我们一般用virtualenv搭建虚拟环境,管理python版本,但是跨平台的使用不太一致,且有时候 ...

  5. jQuery.on() 函数

    1.绑定所有的<p>元素// 为所有P元素分别绑定click事件处理函数handler$("p").on("click", handler); 2. ...

  6. solr 6.6 基础环境搭建 (一)

    Apache Solr 介绍 参考博主原文链接1:http://www.cnblogs.com/blueskyli/p/7100443.html 参考博主原文链接2:http://www.cnblog ...

  7. css3如何实现圆角边框

    圆角边框是css3新增属性,在圆角边框出现之前,前端开发有的采用整块的圆角图片作为背景,有的采用小的圆角图片分别放在元素的四角,非常麻烦,灵活性差,也达到降低了网站的整体性能,而圆角边的出现则降低了开 ...

  8. 【学习笔记】--- 老男孩学Python,day16-17 初识面向对象,类名称空间,查询顺序,组合

    面向过程 VS 面向对象 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优点是:极大的降低了写程序的复 ...

  9. MPVUE - 使用vue.js开发微信小程序

    MPVUE - 使用vue.js开发微信小程序 什么是mpvue? mpvue 是美团点评前端团队开源的一款使用 Vue.js 开发微信小程序的前端框架.框架提供了完整的 Vue.js 开发体验,开发 ...

  10. vue-video监听touch事件

    vue-video是基于 Vue 的简洁 HTML5 视频播放器组件,但是并没有监听touch事件,也就是说在移动端按键无效. 本文讲述如何改写其vue组件,使其兼容移动端.只需要在其原有的mouse ...