ioslabel阴影_iOS-给UIView、UIImageView、UIlabel设置阴影效果

如果UIImageView、只添加阴影不设置圆角可使用:介绍下加阴影几个属性的概念

imageView.layer.shadowColor = [UIColor blackColor].CGColor;

// 设置阴影偏移量

imageView.layer.shadowOffset = CGSizeMake(0,0);

// 设置阴影透明度

imageView.layer.shadowOpacity = 1;

// 设置阴影半径

imageView.layer.shadowRadius = 5;

imageView.clipsToBounds = NO;

介绍两个概念:

clipsToBounds

是指视图上的子视图,如果超出父视图的部分就截取掉

masksToBounds

却是指视图的图层上的子图层,如果超出父图层的部分就截取掉

测试图如下:

VIew加阴影.png

示例代码

UIView加阴影:可直接加阴影

- (void)createViewShadow {

CGFloat margin = ([UIScreen mainScreen].bounds.size.width - width * 3 - leftMargin * 2)/2.f;

UIView *view1 = [[UIView alloc]init];

view1.frame = CGRectMake(leftMargin, width + leftMargin*1.5, width, width);

[self.view addSubview:view1];

self.view1 = view1;

view1.backgroundColor = [UIColor yellowColor];

view1.layer.shadowColor = [UIColor blackColor].CGColor;

view1.layer.shadowOffset = CGSizeMake(-shadowRadius,-shadowRadius);

view1.layer.shadowOpacity = 1;

view1.layer.shadowRadius = shadowRadius;

view1.layer.cornerRadius = shadowRadius;

view1.layer.masksToBounds = YES;

view1.clipsToBounds = NO;

UIView *view2 = [[UIView alloc]init];

view2.frame = CGRectMake(leftMargin + (width + margin), width + leftMargin*1.5, width, width);

[self.view addSubview:view2];

self.view2 = view2;

view2.backgroundColor = [UIColor yellowColor];

view2.layer.shadowColor = [UIColor blackColor].CGColor;

view2.layer.shadowOffset = CGSizeMake(0,0);

view2.layer.shadowOpacity = 1;

view2.layer.shadowRadius = shadowRadius;

view2.layer.cornerRadius = shadowRadius;

view2.layer.masksToBounds = YES;

view2.clipsToBounds = NO;

UIView *view3 = [[UIView alloc]init];

view3.frame = CGRectMake(leftMargin + (width + margin)*2, width + leftMargin*1.5, width, width);

[self.view addSubview:view3];

self.view3 = view3;

view3.backgroundColor = [UIColor yellowColor];

view3.layer.shadowColor = [UIColor blackColor].CGColor;

view3.layer.shadowOffset = CGSizeMake(shadowRadius,shadowRadius);

view3.layer.shadowOpacity = 1;

view3.layer.shadowRadius = shadowRadius;

view3.layer.cornerRadius = shadowRadius;

view3.layer.masksToBounds = YES;

view3.clipsToBounds = NO;

}

UIImageView加阴影:需要加背景View加阴影、imageView裁圆角

- (void)createImageViewShadow {

UIView *bgView = [[UIView alloc]init];

bgView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - width)/2.f, (width + leftMargin*1.5)*2, width, width);

[self.view addSubview:bgView];

self.bgView1 = bgView;

bgView.backgroundColor = [UIColor yellowColor];

bgView.layer.shadowColor = [UIColor blackColor].CGColor;

bgView.layer.shadowOffset = CGSizeMake(0,0);

bgView.layer.shadowOpacity = 1;

bgView.layer.shadowRadius = shadowRadius;

bgView.layer.cornerRadius = shadowRadius;

bgView.layer.masksToBounds = YES;

bgView.clipsToBounds = NO;

UIImageView *imageView = [[UIImageView alloc]init];

imageView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - width)/2.f, (width + leftMargin*1.5 )*2, width, width);

[self.view addSubview:imageView];

imageView.image = [UIImage imageNamed:@"shadow.jpg"];

imageView.layer.cornerRadius = shadowRadius;

imageView.layer.masksToBounds = YES;

}

UILabel加阴影:需要加背景View加阴影、label裁圆角

- (void)createLabelShadow {

UIView *bgView = [[UIView alloc]init];

bgView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - width)/2.f, (width + leftMargin*1.5)*3, width, width);

[self.view addSubview:bgView];

self.bgView2 = bgView;

bgView.backgroundColor = [UIColor yellowColor];

bgView.layer.shadowColor = [UIColor blackColor].CGColor;

bgView.layer.shadowOffset = CGSizeMake(0,0);

bgView.layer.shadowOpacity = 1;

bgView.layer.shadowRadius = shadowRadius;

bgView.layer.cornerRadius = shadowRadius;

bgView.layer.masksToBounds = YES;

bgView.clipsToBounds = NO;

UILabel *label = [[UILabel alloc]init];

label.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - width)/2.f, (width + leftMargin*1.5)*3, width, width);

[self.view addSubview:label];

label.backgroundColor = [UIColor redColor];

label.layer.cornerRadius = shadowRadius;

label.layer.masksToBounds = YES;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值