zoukankan      html  css  js  c++  java
  • 设置 UITextField 的占位符的颜色和光标颜色

    这是在 UITextField 类中
    /**
    
        运行时 :runtime
        可以访问隐藏的一些属性
    
     */
    
    + (void)initialize
    {
        [self getIvars];
        [self getProperties];
    }
    
    //获取所有属性
    + (void)getProperties
    {
        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  <---->  %s",property_getName(property), property_getAttributes(property));
        }
    
    }
    
    //获取所有成员变量列表
    + (void)getIvars
    {
        unsigned int count = 0;
        //拷贝出所有的成员变量列表  这是一个数组 可以访问隐藏的属性
       Ivar *ivars = class_copyIvarList([UITextField class], &count);
        for (int i = 0; i < count; i ++) {
    //        Ivar ivar = *(ivars + i);
            Ivar ivar = ivars[i]; //等同上一句代码
            NSLog(@"%s",ivar_getName(ivar));
        }
        //释放内存 因为带有 copy
        free(ivars);
    }
    
    //视图加载出来时
    - (void)awakeFromNib
    {
    //    UILabel *placeHolderLabel = [self valueForKey:@"_placeholderLabel"];
    //    placeHolderLabel.textColor = [UIColor redColor];
    
    //通过 kvc 赋值
    //    [self setValue:[UIColor orangeColor] forKeyPath:@"_placeholderLabel.textColor"];
    //设置光标颜色和文字颜色一致
    
        self.tintColor = self.textColor;
        [self resignFirstResponder];
    
    }
    
    //文本框成为第一响应者 和放弃 时 设置文本框的 占位符的颜色
    - (BOOL)becomeFirstResponder
    {
        [self setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
        return [super becomeFirstResponder];
    }
    
    - (BOOL)resignFirstResponder
    {
        [self setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        return [super resignFirstResponder];
    }
    
    //外界可以通过访问这个属性 进行赋值
    - (void)setPlaceHolderColor:(UIColor *)placeHolderColor
    {
        _placeHolderColor = placeHolderColor;
        [self setValue:placeHolderColor forKeyPath:@"_placeholderLabel.textColor"];
    
    }
  • 相关阅读:
    wait
    iOS UITableviewCell优化
    iOS本地版本和服务器对比
    iOS 二维码生成 改变颜色 添加中心图
    iOS坑点解析
    iOS View快照,View截屏
    双缓冲读感感悟
    查找附近点--Geohash方案讨论
    各种报告word模板
    跳转到设置里面各个页面iOS8
  • 原文地址:https://www.cnblogs.com/arenouba/p/5428795.html
Copyright © 2011-2022 走看看