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;
//圖層的大小(寬度固定根據(jù)文字定高度)
CGRect strRect = [attrStr boundingRectWithSize:CGSizeMake(150, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil];
textLayer.bounds = CGRectMake(0, 0, strRect.size.width, strRect.size.height + 5);//文本大小,默認(rèn)是0
textLayer.alignmentMode = @"center";//文字對(duì)齊方式
textLayer.wrapped = YES;//是否折行,默認(rèn)是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";//文字對(duì)齊方式
//字體 (僅限于非富文本才能用)
UIFont *fontTemp = [UIFont fontWithName:@"Heiti SC" size:18];
CFStringRef fontName = (__bridge CFStringRef)fontTemp.fontName;
textLayer.font = CGFontCreateWithFontName(fontName);
textLayer.fontSize = fontTemp.pointSize;
//圖層的大小(寬度固定根據(jù)文字定高度)
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);//文本大小,默認(rèn)是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;//是否折行,默認(rèn)是no
textLayer.contentsScale = [UIScreen mainScreen].scale;//清晰度
textLayer.position = CGPointMake(100,350);
[self.view.layer addSublayer:textLayer];
}
}