NSShadow 保存绘制阴影的类,iOS和macOS都能用,iOS中主要用来为富文本设置阴影,参考iOS技术文档No.10 UIKit_NSAttributedString、NSMutableAttributedString iOS富文本
里面就这几条属性:
NS_CLASS_AVAILABLE_IOS(6_0) @interface NSShadow : NSObject <NSCopying, NSCoding>
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@property (nonatomic, assign) CGSize shadowOffset; // offset in user space of the shadow from the original drawing
@property (nonatomic, assign) CGFloat shadowBlurRadius; // blur radius of the shadow in default user space units
@property (nullable, nonatomic, strong) id shadowColor; // color used for the shadow (default is black with an alpha value of 1/3)
代码:
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[UIColor whiteColor]];
[shadow setShadowOffset:CGSizeMake(1.0f, 1.0f)];
[shadow setShadowBlurRadius:4.0];
如果你想为view设置阴影的话建议用下面的方法:
UIView *yellowView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
yellowView.backgroundColor=[UIColor yellowColor];
yellowView.center=self.view.center;
yellowView.layer.shadowColor=[UIColor blackColor].CGColor;
yellowView.layer.shadowOffset=CGSizeMake(2, 2);
yellowView.layer.shadowOpacity=2;
yellowView.layer.shadowRadius=1;
[self.view addSubview:yellowView];
我试图为view引用shadow的属性,但是没有效果
参考: iPhone之为UIView设置阴影(CALayer的shadowColor,shadowOffset,shadowOpacity,shadowRadius,shadowPath属性)