美文网首页程序员iOS技能iOS Developer
用runtime自定义UITextField隐藏属性

用runtime自定义UITextField隐藏属性

作者: Oniityann | 来源:发表于2016-03-05 12:42 被阅读461次

    前言

    系统自带UITextField的光标是默认深蓝色的, placeholder是灰色的, 在开发中, 一定程度上可能影像UI的美观程度, 所以我们需要自定义一个textField来完善界面.
    而在textField自带方法和属性中是没有关于placeholder属性或是方法的, 仅有Attributes可以修改, 或者可以自己创建label来覆盖placeholder, 还可以自己draw一个, 但是个人认为是没有runtime来的方便的.


    Runtime使用简介

    运行时(Runtime):
    苹果官方的一套C语言库
    利用Runtime可以查看很多底层隐藏内容
    比如一些成员变量 / 方法


    利用Runtime查看UITextField隐藏成员变量

    首先, 需要在自定义UITextField头文件处导入:

     #import <objc/runtime.h>
    

    在初始化方法中敲入如下代码:

    + (void)initialize {
        
        unsigned int count = 0;
        
        //拷贝出所有成员变量列表
        Ivar *ivars = class_copyIvarList([UITextField class], &count);
        
        for (int i = 0; i < count; i++) {
            
            //取出成员变量
            Ivar ivar = *(ivars + i);
            
            //打印成员变量名字
            NSLog(@"%s", ivar_getName(ivar));
        }
        
        //释放
        free(ivars);
    }
    

    此时要注意, 尽管在ARC模式下, 取出变量后要依然手动释放内存, 利用free()方法即可:
    free(...)

    运行程序后, 控制台会输出如下隐藏内容:

    控制台输出

    修改placeholder的颜色

    创建全局变量

    根据控制台输出内容找到自己需要的成员变量后, 可以使用KVC修改
    首先创建一个全局变量, 例如我们需要修改placeholder的颜色:

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

    通过KVC改写

    然后通过KVC在第一响应方法中修改(注意是****setValue: forKeyPath: ****方法):

    
    - (BOOL)becomeFirstResponder {
        
        [self setValue:self.textColor forKeyPath:placeholderColorKeyPath];
        
        return [super becomeFirstResponder];
    }
    
    - (BOOL)resignFirstResponder {
        
        //修改占位文字颜色
        [self setValue:[UIColor grayColor] forKeyPath:placeholderColorKeyPath];
        
        return [super resignFirstResponder];
    }
    
    

    此时再运行程序可以发现选中和未选中时的placeholder颜色变得不一样了.


    修改输入框光标颜色

    在textField中, 是没有indicator这个属性的, 光标颜色其实就是tintColor, 例如:

    - (void)awakeFromNib {
        
        //设置光标颜色和文字颜色一致
        self.tintColor = self.textColor;
        
        //在进入页面时是没有选中的要调用resign方法
        [self resignFirstResponder];
    }
    

    查看其它属性

    同ivar一样, 可以使用runtime查看properties

    + (void)initialize {
        unsigned int count = 0;
        
        objc_property_t *properties = class_copyPropertyList([UITextField class], &count);
        
        for (int i = 0; i < count; i++) {
            objc_property_t property = properties[i];
            
            NSLog(@"%s", property_getName(property));
        }
        
        free(properties);
    }
    

    记住不要忘记free()方法


    完整代码

    #import "CustomField.h"
    #import <objc/runtime.h>
    
    static NSString *const placeholderColorKeyPath = @"_placeholderLabel.textColor";
    
    @implementation CustomField
    
    #if 0
    + (void)initialize {
        
        unsigned int count = 0;
        
        //拷贝出所有成员变量列表
        Ivar *ivars = class_copyIvarList([UITextField class], &count);
        
        for (int i = 0; i < count; i++) {
            
            //取出成员变量
            Ivar ivar = *(ivars + i);
            
            //打印成员变量名字
            NSLog(@"%s", ivar_getName(ivar));
        }
        
        //释放
        free(ivars);
    }
    #endif
    
    #if 0
    /**
     找出系统隐藏属性
     */
    + (void)initialize {
        unsigned int count = 0;
        
        objc_property_t *properties = class_copyPropertyList([UITextField class], &count);
        
        for (int i = 0; i < count; i++) {
            objc_property_t property = properties[i];
            
            NSLog(@"%s", property_getName(property));
        }
        
        free(properties);
    }
    #endif
    
    - (void)awakeFromNib {
        
        //设置光标颜色和文字颜色一致
        self.tintColor = self.textColor;
        
        [self resignFirstResponder];
    }
    
    - (BOOL)becomeFirstResponder {
        
        [self setValue:self.textColor forKeyPath:placeholderColorKeyPath];
        
        return [super becomeFirstResponder];
    }
    
    - (BOOL)resignFirstResponder {
        
        //修改占位文字颜色
        [self setValue:[UIColor grayColor] forKeyPath:placeholderColorKeyPath];
        
        return [super resignFirstResponder];
    }
    
    @end
    
    

    相关文章

      网友评论

        本文标题:用runtime自定义UITextField隐藏属性

        本文链接:https://www.haomeiwen.com/subject/znookttx.html