CALayer及其子类CAShapeLayer, CATextLayer用来展示图片,文本,富文本,绘图等, 完全可以替代UIImageView,UILabel等常用控件
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//layer展示图片
CALayer *layerTemp = [CALayer layer];
layerTemp.frame = CGRectMake(80, 100, 20, 20);
[self.view.layer addSublayer:layerTemp];
layerTemp.contents = (id)[UIImage imageNamed:@"common_icon_msg_error"].CGImage;
CALayer *layerTemp2 = [CALayer layer];
layerTemp2.frame = CGRectMake(80, 150, 60, 64);
[self.view.layer addSublayer:layerTemp2];
layerTemp2.contents = (id)[UIImage imageNamed:@"analysis_cup"].CGImage;
//绘图
CAShapeLayer * shapeLayer = [CAShapeLayer layer];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,nil,0.0,0);
CGPathAddLineToPoint(path,nil,0.0,CGRectGetHeight(self.view.bounds)/2);
shapeLayer.path = path;
shapeLayer.bounds = CGRectMake(0,0,5.0,CGRectGetHeight(self.view.bounds)/2);
shapeLayer.anchorPoint = CGPointMake(0.5, 0.5);
shapeLayer.position = CGPointMake(CGRectGetMidX(self.view.bounds)+100,CGRectGetMidY(self.view.bounds));
shapeLayer.lineWidth = 5.0;
shapeLayer.lineCap = kCALineCapRound;
shapeLayer.strokeColor = [UIColor colorWithRed:0.153 green:1.000 blue:0.460 alpha:1.000].CGColor;
[self.view.layer addSublayer:shapeLayer];
//文本展示
if (/* DISABLES CODE */ (1)) {//富文本
CATextLayer * textLayer = [CATextLayer layer];
NSString * text = @"今天天气好晴朗,嘿!处处百花香,嘿嘿嘿!!!明天星期二,后天就是星期三,再有两天就又放假咯!吼吼吼~~~";
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];
NSDictionary *attributeDict = @{NSFontAttributeName: [UIFont systemFontOfSize:18.0],
NSForegroundColorAttributeName: [UIColor redColor]};
NSDictionary *attributeDict1 = @{NSFontAttributeName: [UIFont systemFontOfSize:23.0],
NSForegroundColorAttributeName: [UIColor colorWithRed:0.130 green:0.854 blue:0.345 alpha:1.000]};
[attrStr setAttributes:attributeDict1 range:NSMakeRange(0, 7)];
[attrStr setAttributes:attributeDict range:NSMakeRange(7, attrStr.length -7)];
textLayer.string = attrStr;
//给图层加边框
textLayer.borderColor = [UIColor colorWithRed:0.193 green:0.607 blue:1.000 alpha:1.000].CGColor;
textLayer.borderWidth = 0.5;
//图层的大小(宽度固定根据文字定高度)
CGRect strRect = [attrStr boundingRectWithSize:CGSizeMake(150, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil];
textLayer.bounds = CGRectMake(0, 0, strRect.size.width, strRect.size.height + 5);//文本大小,默认是0
textLayer.alignmentMode = @"center";//文字对齐方式
textLayer.wrapped = YES;//是否折行,默认是no
textLayer.contentsScale = [UIScreen mainScreen].scale;//清晰度
textLayer.position = CGPointMake(100,350);//位置
[self.view.layer addSublayer:textLayer];
} else {//非富文本
CATextLayer * textLayer = [CATextLayer layer];
NSString * text = @"今天天气好晴朗,嘿!处处百花香,嘿嘿嘿!!!明天星期二,后天就是星期三,再有两天就又放假咯!吼吼吼~~~";
textLayer.string = text;
textLayer.alignmentMode = @"center";//文字对齐方式
//字体 (仅限于非富文本才能用)
UIFont *fontTemp = [UIFont fontWithName:@"Heiti SC" size:18];
CFStringRef fontName = (__bridge CFStringRef)fontTemp.fontName;
textLayer.font = CGFontCreateWithFontName(fontName);
textLayer.fontSize = fontTemp.pointSize;
//图层的大小(宽度固定根据文字定高度)
CGRect strRect = [text boundingRectWithSize:CGSizeMake(150, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName : fontTemp} context:nil];
textLayer.bounds = CGRectMake(0, 0, strRect.size.width, strRect.size.height + 5);//文本大小,默认是0
//给图层加边框
textLayer.borderColor = [UIColor colorWithRed:0.193 green:0.607 blue:1.000 alpha:1.000].CGColor;
textLayer.borderWidth = 0.5;
//字体颜色(仅限于非富文本才能用)
textLayer.foregroundColor = [UIColor colorWithRed:0.150 green:0.301 blue:0.791 alpha:1.000].CGColor;
textLayer.wrapped = YES;//是否折行,默认是no
textLayer.contentsScale = [UIScreen mainScreen].scale;//清晰度
textLayer.position = CGPointMake(100,350);
[self.view.layer addSublayer:textLayer];
}
}