UIView设置圆角和阴影
2018-09-29 本文已影响8人
枫叶1234
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = kUIColorFromRGB(0x4b4773);
//v.layer.masksToBounds=YES;这行去掉
self.layer.cornerRadius = 10;
self.layer.shadowColor = kUIColorFromRGB1(0x000000, 1).CGColor;
self.layer.shadowOffset = CGSizeMake(2, 5);
self.layer.shadowOpacity = 0.5;
self.layer.shadowRadius = 5;
}
return self;
}
用代码设置cornerRadius,不用设置masksToBounds 这个属性
阴影
ShadowOffset 这个属性的用法,
x向右为正,y向下为正
1.y<0
UILabel *label=[[UILabelalloc]
initWithFrame:CGRectMake(40,40,
250,50)];
label.backgroundColor=[UIColorclearColor];
label.textColor=[UIColorredColor];
label.text=@"和合谷";
label.shadowColor=[UIColorblueColor];
label.shadowOffset=CGSizeMake(0, -3);
运行效果如下
20140506105608125.png
第二.y>0
UILabel *label=[[UILabelalloc]
initWithFrame:CGRectMake(40,40,
250,50)];
label.backgroundColor=[UIColorclearColor];
label.textColor=[UIColorredColor];
label.text=@"和合谷";
label.shadowColor=[UIColorblueColor];
label.shadowOffset=CGSizeMake(0,3);
运行效果如下
20140506105805656.png
第三、x>0
UILabel *label=[[UILabelalloc]
initWithFrame:CGRectMake(40,40,
250,50)];
label.backgroundColor=[UIColorclearColor];
label.textColor=[UIColorredColor];
label.text=@"和合谷";
label.shadowColor=[UIColorblueColor]; label.shadowOffset=CGSizeMake(3,0);
[self.viewaddSubview:label];
运行效果如下
20140506105933171.png
第四、x<0
UILabel *label=[[UILabelalloc]
initWithFrame:CGRectMake(40,40,
250,50)];
label.backgroundColor=[UIColorclearColor];
label.textColor=[UIColorredColor];
label.text=@"和合谷";
label.shadowColor=[UIColorblueColor];
label.shadowOffset=CGSizeMake(-3,0);
运行效果如下
20140506110142015.png
注意:
如果想给uiview的四周都有阴影效果,可以设置layer.shadowOffset = CGSizeMake(0,0);
只设置layer.shadowRadius 就可以了
例如:下面的效果
屏幕快照 2018-09-29 下午2.00.43.png
看起来是四周都有阴影,是下面代码:
_bgView.layer.cornerRadius = 10.;
_bgView.layer.shadowColor = [UIColor blackColor].CGColor;
_bgView.layer.shadowOpacity = .1;
_bgView.layer.shadowRadius = 2.f;
_bgView.layer.shadowOffset = CGSizeMake(0.f, 0.f);