美文网首页IOS知识整理
用KVC进行属性设置

用KVC进行属性设置

作者: 跃文 | 来源:发表于2019-04-16 14:29 被阅读0次

UIAlertController 设置

UIAlertController *alertC ;
UIAlertAction *action_cancel ;
UIAlertAction *action_sure;
//修改 title属性
NSMutableAttributedString * titleAttStr = [[NSMutableAttributedString alloc] initWithString:@"支付"];
[titleAttStr setAttributes:@{NSFontAttributeName : FONT_BOLD(16.0), NSForegroundColorAttributeName : ColorFromHex(@"4a4a4a")} range:NSMakeRange(0, titleAttStr.length)];
[alertC setValue:titleAttStr forKey:@"attributedTitle"];
//修改 message属性  
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = 10;
style.alignment = NSTextAlignmentCenter;
NSMutableAttributedString * messageAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"余额支付\n¥ %@ 元", self.donatedAmount]];
[messageAttStr setAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13], NSForegroundColorAttributeName : ColorFromHex(@"4a4a4a"), NSParagraphStyleAttributeName : style} range:NSMakeRange(0, 4)];
[messageAttStr setAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:25],  NSForegroundColorAttributeName : ColorFromHex(@"4a4a4a"), NSParagraphStyleAttributeName : style} range:NSMakeRange(7, messageAttStr.length - 7)];
[alertC setValue:messageAttStr forKey:@"attributedMessage"];
// 修改按钮颜色
[action_sure setValue:ColorFromHex(@"96b057") forKey:@"titleTextColor"];
[action_cancle setValue:ColorFromHex(@"9b9b9b") forKey:@"titleTextColor"];

UITextField 设置

UITextField *textField;
// 方法一:KVC
textField.placeholder = @"this is placeholder";
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
// 方法二:属性字符串
field.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"placeholder attribute" attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName:[UIColor redColor]}];

// 方法三:对于UITextField修改也可以获取placeHolder对应的UILabel进行修改

UILabel *placeholderLabel = [textField valueForKey:@"_placeholderLabel"];

注: textField.fontplaceholderLabel.font 大小不同会引发的placeholder的偏移,调整方法--新建子类重写父类方法

- (CGRect)placeholderRectForBounds:(CGRect)bounds {
// 返回值就是调整后placeholderLabel的位置
    return CGRectMake(0, 0 , self.bounds.size.width, self.bounds.size.height);
}
- (void)drawPlaceholderInRect:(CGRect)rect {
// 此处设置的rect是绘制placeholder在placeholderLabel中的frame
// offSetY是绘制placeholderLabel 的向下偏移量
    [super drawPlaceholderInRect:CGRectMake(0, offSetY , self.bounds.size.width, self.bounds.size.height)];
}

补充:在需要对UITextField进行圆角裁剪时,我们会发现光标位置会贴近圆角,这是我的处理可以使用以下两种方法

1.在子类中重写父类方法
// 输入前文本向右缩进10
- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 10, 0);
}
// 输入后文本向右缩进10
- (CGRect)editingRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 10, 0);
}

2.添加一个左视图
UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];// 宽度为10的占位视图
textFiled.leftView = leftView;
textFiled.leftViewMode = UITextFieldViewModeAlways;

获取类对应属性、方法的方法

- (void)getAllPropertyAndAllMethod:(id)myClass {
    
    unsigned int outCount = 0;
    // 获取到所有的成员变量列表
    Ivar *vars = class_copyIvarList([myClass class], &outCount);
    // 遍历所有的成员变量
    for (int i = 0; i < outCount; i++) {
        Ivar ivar = vars[i]; // 取出第i个位置的成员变量
        
        const char *propertyName = ivar_getName(ivar); // 获取变量名
        const char *propertyType = ivar_getTypeEncoding(ivar); // 获取变量编码类型
        NSLog(@"property : %s/%s\n", propertyName, propertyType);
    }
    
    unsigned int count;
    //获取方法列表,所有在.m文件显式实现的方法都会被找到,包括setter+getter方法;
    Method *allMethods = class_copyMethodList([myClass class], &count);
    for(int i =0;i<count;i++)
    {
        //Method,为runtime声明的一个宏,表示对一个方法的描述
        Method md = allMethods[i];
        //获取SEL:SEL类型,即获取方法选择器@selector()
        SEL sel = method_getName(md);
        //得到sel的方法名:以字符串格式获取sel的name,也即@selector()中的方法名称
        const char *methodname = sel_getName(sel);
        
        NSLog(@"(Method : %s)",methodname);
    }
}

相关文章

  • 用KVC进行属性设置

    UIAlertController 设置 UITextField 设置 注: textField.font 和 p...

  • iOS开发中的一些小技巧

    1、取出UISearchBar中的控件进行属性设置 类似的,按钮之类的控件也可以用KVC进行对私有属性赋值或者更改...

  • KVC与KVO

    KVC-键值编码(Key Value Coding) 动态设置:setValue:属性值 forKey:属性名(用...

  • iOS之KVC与KVO

    KVC: kvc:Key Value Coding,即键值编码,可以动态设置的对象的属性值。 动态设置设置属性,优...

  • KVC的简单使用

    KVC字典转模型 KVC 中经常使用的就是字典转模型 KVC的大招 KVC设置对象属性及取值 KVC间接设置对象属...

  • iOS KVC键值编码

    KVC键值编码,这个功能很强大,它可以改变私有属性和只读属性。 KVC操作方法如下: 设置setValue:属性值...

  • KVO&KVC

    KVC KVC: key value coding 键值编码 通过KVC语法,可以修改对象中的属性。设置值时:例...

  • KVC 相关

    1 .KVC 的基本使用kvc 相关的基本方法包括 设置属性值:::setValue: forKey: ;se...

  • iOS - 属性不被外部篡改

    当属性的修饰符为readonly,表示该属性为只读,那么能否修改这个属性的值呢? 外部通过KVC设置与禁止KVC设...

  • 05. KVC的使用,原理,本质

    问题 常用的KVC的API; KVC设置值原理图; KVC获取值原理图; 通过KVC修改属性会触发KVO吗? 答案...

网友评论

    本文标题:用KVC进行属性设置

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