UITextFeild的用法
一. 修改占位字符串的 颜色:
=======方法一 ======================================
#import "ViewController.h"
#import "MyTextField.h"
@interface ViewController ()
{
UITextField *_textF;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
MyTextField *textF = [[MyTextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
_textF = textF;
[self.view addSubview:textF];
textF.backgroundColor = [UIColor whiteColor];
textF.borderStyle = UITextBorderStyleRoundedRect;
textF.leftViewMode = UITextFieldViewModeWhileEditing;
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
btn.backgroundColor=[UIColor greenColor];
textF.leftView = btn;
textF.placeholder = @"哈哈";
UIColor *color = [UIColor colorWithRed:100/255.0 green:200/255.0 blue:100/255.0 alpha:0.7];
[textF setValue:color forKeyPath:@"_placeholderLabel.textColor"];//修改占位字符串“哈哈”的颜色
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[_textF resignFirstResponder];
}
@end
运行效果:
1. 没有进入编辑状态效果(textF.leftView = btn;隐藏):
2. 进入编辑状态效果(textF.leftView = btn;出现):
======= 方法二 =================================
#import "MyTextField.h"
@implementation MyTextField
- (void)drawPlaceholderInRect:(CGRect)rect{
UIColor *placeholderColor = [UIColor redColor];//设置颜色
[placeholderColor setFill];
CGRect placeholderRect = CGRectMake(rect.origin.x+30, (rect.size.height- self.font.pointSize)/5, rect.size.width, self.font.pointSize);//设置距离
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByTruncatingMiddle;
style.alignment = self.textAlignment;
NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName, self.font, NSFontAttributeName, placeholderColor, NSForegroundColorAttributeName, nil];
[self.placeholder drawInRect:placeholderRect withAttributes:attr];
}
@end
#import "ViewController.h"
#import "MyTextField.h"
@interface ViewController ()
{
UITextField *_textF;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
MyTextField *textF = [[MyTextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
_textF = textF;
[self.view addSubview:textF];
textF.backgroundColor = [UIColor whiteColor];
textF.borderStyle = UITextBorderStyleRoundedRect;
textF.leftViewMode = UITextFieldViewModeWhileEditing;
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
btn.backgroundColor=[UIColor greenColor];
textF.leftView = btn;
textF.placeholder = @"哈哈哈哈哈哈";
// UIColor *color = [UIColor colorWithRed:100/255.0 green:200/255.0 blue:100/255.0 alpha:0.7];
// [textF setValue:color forKeyPath:@"_placeholderLabel.textColor"];//修改占位字符串“哈哈”的颜色
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[_textF resignFirstResponder];
}
@end
运行效果:
UITextFeild的用法的更多相关文章
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
- 【JavaScript】innerHTML、innerText和outerHTML的用法区别
用法: <div id="test"> <span style="color:red">test1</span> tes ...
- chattr用法
[root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...
- 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)
vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...
随机推荐
- Android获取内置sdcard跟外置sdcard路径
Android获取内置sdcard跟外置sdcard路径.(测试过两个手机,亲测可用) 1.先得到外置sdcard路径,这个接口是系统提供的标准接口. 2.得到上一级文件夹目录 3.得到该目录的所有文 ...
- java删除文件夹
想删除本地一个项目目录,结果windows说路径太长,不能删除.于是试了试java删除.一切ok.以后一定要抓紧时间学python. /** * Created by rmiao on 4/21/20 ...
- JavaScript中的数据类型转换
本文中提到的“原始值”指的是undefined,null,Boolean,string和number. 本文中的对象是native对象,宿主对象(浏览器定义的对象)按照各自的算法转换. JavaScr ...
- Autofac - 装配
从容器中的可用服务中, 选取一个构造函数来创造对象, 这个过程就是自动装配. 一.选择构造函数 默认情况下, autofac会使用无参构造函数, 去创建对象. 我将Person类稍微修改了下. pub ...
- 解决.NET WebService引用后添加HTTP Header的问题
麻蛋,搜索了好久,找到的都是对soap header的操作,不是对WebService的HTTP Header的操作,这是两种不同的概念,平常我们发起的WebService请求走的都是http通信协议 ...
- [转]DbFirst数据验证
转自:Data Validate 之 Data Annotation 什么是Data Annotation ? 如何使用 ? 自定义Validate Attribute EF Db first中使用 ...
- XAF视频教程来啦,已出7课
XAF交流学习群内的兄弟录制了视频,他没有博客,委拖我发至博客园,希望能让更多的开发人员受益.快速开发企业级应用的好工具! XAF入门01快速浏览 XAF入门02特点. XAF入门03 ...
- for xml path 将单表中一个字段用逗号分隔
我也是才知道这种用法的,刚好又用到写个简单的例子. select Name from tc_order_detail 如下表,现在要将做到将name每个以逗号连接 declare @df nvarch ...
- JavaScript一词被《牛津大词典》收录了
早上看VS Team的推特发了这个图片,以前总爱问Java怎么读,现在好了,有标准发音了. 确定是 扎瓦·死磕瑞普特 ,哈哈,以后不要再念加瓦了. …… Last month JavaScript r ...
- [修正] Firemonkey TSelection 控件等比缩放时,左下角拉动问题
说明:TSelection 控件,当在属性 Proportional = True 为等比缩放时,拉动左下角,右上角会跟着移动. 适用:Berlin 10.1.1 (或之前版本) Firemonkey ...