setValue: forKeyPath:

来源:互联网 发布:软件他电脑效果器 编辑:程序博客网 时间:2024/06/15 00:44


通过 setValue:    forKeyPath:
这个方法来设置一些属性信息      

   

<span style="font-size:18px;"> UITextField *textfield = [UITextField new]; [textfield setValue:[UIColor redcolor] forKeyPath:@"_placeholderLabel.textColor"];[self setUIRectCorner:textfield</span><span style="font-size:18px;">];[self.view addSubview:textfield];</span>

此处的KeyPath所填写的内容有一个特点,首先是你想要修改的控件里面有这个分支,像UITextField里面就包含placeholderLabel,然后就是针对Label的属性,所以就得到了keyPath的内容“_placeholderLabel.textColor


要是以后有些控件的一些信息无法改变的时候,你可以考虑用一下这个方法来尝试一下


继续上面的内容,一下是设置UITextField一边圆角一边直角的方法


<span style="font-size:18px;">- (void)setUIRectCorner:(UIView*)view {    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds                                                   byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft                                                         cornerRadii:CGSizeMake(10.0, 10.0)];//10.是给圆角的设置    CAShapeLayer *maskLayer    = [CAShapeLayer layer];    CAShapeLayer *borderLayer  =[CAShapeLayer layer];        borderLayer.path           = maskPath.CGPath;    borderLayer.fillColor      = [UIColor clearColor].CGColor;    borderLayer.strokeColor    = [UIColor orangeColor].CGColor;    borderLayer.lineWidth      = 1.5;    borderLayer.frame          =view.bounds;        maskLayer.path             = maskPath.CGPath;        view.layer.mask = maskLayer;        [view.layer addSublayer:borderLayer];}//线面图片就是展示效果以及附带代码</span>







0 0
原创粉丝点击