设置placeholder的颜色

news/2024/7/20 20:35:27 标签: iOS, UITextField, placeholder

方法一:自定义UITextField类,重写drawPlaceholderInRect方法

- (void)drawPlaceholderInRect:(CGRect)rect

{

    [self.placeholder drawInRect:CGRectMake(0, 10,rect.size.width, 25) withAttributes:@{

                            NSForegroundColorAttributeName:[UIColor whiteColor],

                                       NSFontAttributeName:self.font

                                                                                    }];

}


方法二:设定属性文字

    // 用NSAttributedString

    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];

    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];

    NSAttributedString *placeholder = [[NSAttributedString alloc]initWithString:@"手机号" attributes:attrs];

    self.phoneField.attributedPlaceholder = placeholder;

    

    // 用NSMutableAttributedString

    NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc]initWithString:@"手机号"];

    [placeholder setAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(0, 1)];

    [placeholder setAttributes:@{

                                 

                                 NSForegroundColorAttributeName:[UIColor redColor],

                                 NSFontAttributeName:[UIFont systemFontOfSize:18],

                                 NSUnderlineStyleAttributeName:@"1"

                                 

                                 } range:NSMakeRange(1, 1)];

    

    [placeholder setAttributes:@{NSForegroundColorAttributeName:[UIColor yellowColor]} range:NSMakeRange(2, 1)];

    self.phoneField.attributedPlaceholder = placeholder;


方法三:重写UITextField,通过修改隐藏的实例变量的值,通过KVC修改

#import "FLTextField.h"

// 导入头文件

#import <objc/runtime.h>

// 定义宏

static NSString *const FLPlaceholderColorForKeyPath = @"_placeholderLabel.textColor";


@implementation FLTextField


- (void)awakeFromNib

{

//    UILabel *paleceLabel = [self valueForKeyPath:@"_placeholderLabel"];

//    paleceLabel.textColor = [UIColor redColor];

//    [self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];

    // 让光标颜色与文字颜色一致

    self.tintColor = self.textColor;

    

    [self resignFirstResponder];    

}


- (BOOL)becomeFirstResponder

{

    [self setValue:[UIColor whiteColor] forKeyPath:FLPlaceholderColorForKeyPath];

    return [super becomeFirstResponder];

}


- (BOOL)resignFirstResponder

{

    [self setValue:[UIColor grayColor] forKeyPath:FLPlaceholderColorForKeyPath];

    return [super resignFirstResponder];

}


// 如何找到隐藏的实例变量

+ (void)initialize

{

    unsigned int count = 0;

   Ivar *ivars =  class_copyIvarList([UITextField class], &count);

    

    for (int i = 0; i< count; i++) {

        // 取出成员变量

        Ivar ivar = *(ivars + i);

        

        // 打印成员变量的值

        FLLog(@"%s",ivar_getName(ivar));

        

    }

    

    // 释放

    free(ivars);

}


@end








http://www.niftyadmin.cn/n/1035864.html

相关文章

BADI修改VL02N屏幕字段

SE18 查看 badi LE_SHP_DELIVERY_PROC .我们会发现系统对这个 badi 有实现了一个类 . 下面我们会参考这个类的方法实现我们的需求 . 如图 .1.SE19创建badi LE_SHP_DELIVERY_PROC的实现: ZI_SHP_DELIVERY_PROC.再创建增强实现: ZIM_SHP_DELIVERY_PROC.之后如下图&#xff0c;比较…

如何实现推送引导

思路篇&#xff1a; 1.图形搭建&#xff1a;自定义View &#xff0b; Xib 2.实现逻辑&#xff1a; 1、将视图加入根视图控制器 2、如何决定显示与否&#xff08;只在程序当前版本第一次打开的时候显示&#xff09;&#xff1a;取到当前版本号&#xff0c;与沙盒中的版本号比较…

android studio apk签名及配置

目录一.keytool 工具使用1.keytool 查看密码2.下载keytool工具&#xff0c;签名二.使用apksigner 进行签名三. android studio 签名系统apk对apk进行签名有两种方式一种是通过android stuido 来进行签名&#xff0c;另一种是通过keytool来进行签名&#xff0c;本篇主要来讲解后…

JS Canvas context API 画布

// Ready var elem document.getElementById("myCanvas"); var context elem.getContext(2d);//2D context API----线条context.fillStyle #00f; //填充色context.fillRect(x,y,width,height); //绘制带填充的矩形 context.strokeStyle #f00; //边框色或轮廓色c…

Android 常用的路径、文件操作

在Android开发过程中&#xff0c;我们经常会对文件系统进行操作——存放、释放我们应用的数据。Android系统中提供了各种功能的文件目录&#xff0c;每个目录都有相应的特点和功能。 通过Context来获取路径 一.在全文获取context的方法 具体思路是在MyApplication里面定义一个…

HTML VIDEO 通过鼠标移动出发play和pause功能

<!doctype html><html><head><meta charset"utf-8"><title>通过鼠标移动出发play和pause功能</title></head><body><video id"movies" controls onMouseOver"this.play()" onMouseOut"…

html5在canvas中插入图片

html5在canvas中插入图片 在canvas中显示图片非常简单。可以通过修正层为图片添加印章、拉伸图片或者修改图片等&#xff0c;并且图片通常会成为canvas上的焦点。用HTML5 Canvas API内置的几个简单命令可以轻松地为canvas添加图片内容。 不过&#xff0c;图片增加了canvas操作的…

java异常处理-java的异常

Java内置了一套异常处理机制&#xff0c;总是使用异常来表示错误。 异常是一种class&#xff0c;因此它本身带有类型信息。异常可以在任何地方抛出&#xff0c;但只需要在上层捕获&#xff0c;这样就和方法调用分离了&#xff1a; try {String s processFile(“C:\\test.txt”…