一. 修改占位字符串的 颜色:

=======方法一 ======================================

#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的用法的更多相关文章

  1. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  2. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  3. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  4. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  5. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  6. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  7. 【JavaScript】innerHTML、innerText和outerHTML的用法区别

    用法: <div id="test">   <span style="color:red">test1</span> tes ...

  8. chattr用法

    [root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...

  9. 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)

    vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...

随机推荐

  1. 给Easyui combobox设定默认值

          今天做到那个北理工二期的项目,里面刚好有几个dialog需要弄一个默认值,一般是选择启用与否,但是,为了方便用户,最好有一个默认值,所以,增加一个默认值的属性.代码入下: JS代码:   ...

  2. iOS 实现类似雷达效果的核心代码

    -(void)drawRect:(CGRect)rect { [[UIColor clearColor]setFill]; UIRectFill(rect); NSInteger pulsingCou ...

  3. swift 中数据类型那个的转换

    在swift中关于数据类型的转换,如果参数是可选类型? 那么打印或者转换的结果 会带有Optional 字样,,

  4. ubunt14.04 安装JDK

    1.到 Sun 的官网下载 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 选择 ...

  5. 1.JAVA中使用JNI调用C++代码学习笔记

    Java 之JNI编程1.什么是JNI? JNI:(Java Natibe Inetrface)缩写. 2.为什么要学习JNI?  Java 是跨平台的语言,但是在有些时候仍然是有需要调用本地代码 ( ...

  6. 在SQL Serve里停用行和页层级锁

    今天我想谈下SQL Server里另一个非常有趣的话题:在SQL Server里停用行和页层级锁.在SQL Server里,每次你重建一个索引,你可以使用ALLOW_ROW_LOCKS 和ALLOW_ ...

  7. LINQ to SQL语句(16)之对象标识

    对象标识 运行库中的对象具有唯一标识.引用同一对象的两个变量实际上是引用此对象的同一实例.你更改一个变量后,可以通过另一个变量看到这些更改. 关系数据库表中的行不具有唯一标识.由于每一行都具有唯一的主 ...

  8. c# asp.net4.0尚未在web服务器上注册

    辗转无数论坛,感谢http://bbs.csdn.net/topics/390992746 microsoft官方文档: https://blogs.msdn.microsoft.com/webdev ...

  9. Oracle循环中的EXIT、RETURN、CONTINUE解密

    有时候编写Oracle中用游标等信息去循环处理逻辑的时候,对EXIT.RETURN.CONTINUE很容易搞混淆,网上搜了资料也不是很清楚,所以本人自己写了一小段代码测试了这三种用法.案例代码如下: ...

  10. Android Studio创建AVD

    Android Studio是专门为Android开发设计的IDE,比Eclipse开发Android更加方便.快捷. 安装Android Studio以后,想运行AVD,出现了下面的错误: 提示没有 ...