我有 SVG 文件,我可以使用SVGKit
访问SVG的形状。
现在,我有CAShapeLayer
可能包含圆形,方形或任何封闭形状。
我想在CATextLayer
上添加CAShapeLayer
,使文字不会超出定义的形状。
以下是在CATextLayer
上交叉CAShapeLayer
的示例:
它只是因为,CATextLayer
从CAShapeLayer
的0位开始,其中包含圆圈,特别是这种情况。
就我而言,CAShapeLayer
可以包含任何封闭的形状。它也可以是椭圆形的。
如何识别CAShapeLayer
内的形状?或者我如何应用CATextLayer
的路径?这将确保文本将在形状内绘制?
以下是在CATextLayer
上添加CAShapeLayer
的代码:
-(CATextLayer *)addTrackerNumberToLayer:(NSString *)numbers{
self.numbersTextLayer = [CATextLayer new];
self.numbersTextLayer.foregroundColor = [UIColor blackColor].CGColor;
self.numbersTextLayer.font = (__bridge CFTypeRef _Nullable)([UIFont boldSystemFontOfSize:15]);
self.numbersTextLayer.fontSize=25;
self.numbersTextLayer.anchorPoint = CGPointZero;
self.numbersTextLayer.frame = CGRectMake(self.parentShapeLayer.bounds.size.width*0.05, self.parentShapeLayer.bounds.size.height*0.05, self.parentShapeLayer.bounds.size.width*0.90, self.parentShapeLayer.bounds.size.height*0.30);
self.numbersTextLayer.position = CGPointMake(self.parentShapeLayer.bounds.size.width*0.05,self.parentShapeLayer.bounds.size.height*0.05);
self.numbersTextLayer.alignmentMode = kCAAlignmentCenter;
[self.parentShapeLayer addSublayer:self.numbersTextLayer];
}
答案 0 :(得分:0)
使用isWrapped
CATextLayer
属性确定文本是否被包装以适合接收者的范围。
-(CATextLayer *)addTrackerNumberToLayer:(NSString *)numbers{
self.numbersTextLayer = [CATextLayer new];
self.numbersTextLayer.foregroundColor = [UIColor blackColor].CGColor;
self.numbersTextLayer.font = (__bridge CFTypeRef _Nullable)([UIFont boldSystemFontOfSize:15]);
self.numbersTextLayer.fontSize=25;
self.numbersTextLayer.anchorPoint = CGPointZero;
self.numbersTextLayer.frame = CGRectMake(self.parentShapeLayer.bounds.size.width*0.05, self.parentShapeLayer.bounds.size.height*0.05, self.parentShapeLayer.bounds.size.width*0.90, self.parentShapeLayer.bounds.size.height*0.30);
self.numbersTextLayer.position = CGPointMake(self.parentShapeLayer.bounds.size.width*0.05,self.parentShapeLayer.bounds.size.height*0.05);
self.numbersTextLayer.alignmentMode = kCAAlignmentCenter;
[self.numbersTextLayer setWrapped:YES];//User Wrapped to YES to imbound the text to fit in the region.
[self.parentShapeLayer setMasksToBounds:YES];
[self.parentShapeLayer addSublayer:self.numbersTextLayer];
return self.numbersTextLayer;
}