您的位置:首页 > 产品设计 > UI/UE

setValue: forKeyPath:

2015-08-07 10:59 225 查看
通过 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>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: