IOS知识整理

用KVC进行属性设置

2019-04-16  本文已影响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);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读