Ios 在UIBezierPath中写入文本

Ios 在UIBezierPath中写入文本,ios,objective-c,Ios,Objective C,我正在尝试做一些非常简单的事情,但是我不知道如何用obj c来做。我想在我的UIBezierPath用作形状的CAShapeLayer中写入字符串“hello world”。你能告诉我这是如何通过代码完成的吗??我只想把这个字符串写在形状里面 我在下面有一条UIBEzier路径: UIBezierPath*oval2Path=[UIBezierPath-bezierpath-withovalinrect:oval2Rect] 在我的viewcontroller中,不知道如何向其中添加文本,为什么

我正在尝试做一些非常简单的事情,但是我不知道如何用obj c来做。我想在我的UIBezierPath用作形状的CAShapeLayer中写入字符串“hello world”。你能告诉我这是如何通过代码完成的吗??我只想把这个字符串写在形状里面

我在下面有一条UIBEzier路径:
UIBezierPath*oval2Path=[UIBezierPath-bezierpath-withovalinrect:oval2Rect]


在我的viewcontroller中,不知道如何向其中添加文本,为什么不在UIView的子类中执行此操作?你可以这样做:

-(void)drawRect{
    UIBezierPath* oval2Path = [UIBezierPath bezierPathWithOvalInRect: self.bounds];
    [oval2Path addClip];
    [@"Hello World" drawInRect:self.bounds withAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:self.bounds.size.height]}];
}
我找到了一些教程(,)来处理核心文本,但从iOS 7开始,您似乎也可以使用
UITextView
和TextKit来处理:

UIBezierPath *exclusionPath = ...; // some bezier path
textView.textContainer.exclusionPaths = @[exclusionPath];

(TextKit解决方案抄袭自。)

在这里,我还使用贝塞尔路径和文本创建了一些圆

for (int i = 0; i < numberOfArcs.count; i++) {
    NSInteger startAngele   =   i * 360/numberOfArcs.count+270;
    NSInteger endAngele;
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];

        [bezierPath addArcWithCenter:CGPointMake(self.bounds.size.width/2.0, self.bounds.size.width/2.0) radius:0.25  * self.bounds.size.width startAngle:degreesToRadians(startAngele) endAngle:degreesToRadians(endAngele) clockwise:YES];
    }
for(int i=0;i
使用CAShapelayer在bezier路径上写入文本,CATextlayer使用以下代码

         CAShapeLayer *progressLayer = [[CAShapeLayer alloc] init];
        [progressLayer setPath:bezierPath.CGPath];


        CATextLayer* text = [CATextLayer new];
        for (int i = 1; i <= numberOfArcs.count; i++) {
        text.string =  [NSString stringWithFormat:@"%i", i];
        text.font = (__bridge CFTypeRef _Nullable)([UIFont fontWithName:@"akaChen" size:42]);
        text.font = (__bridge CFTypeRef _Nullable)([UIFont boldSystemFontOfSize:15]);
        text.fontSize=25;
        text.frame = CGRectMake(0,0,40,40);
        text.position = CGPointMake(CGRectGetMidX(progressLayer.frame) ,CGRectGetMidY(progressLayer.frame) );

        CGFloat vert = CGRectGetMidY(progressLayer.frame) / CGRectGetHeight(text.frame);

        text.anchorPoint = CGPointMake(0.5, vert );
        text.alignmentMode = kCAAlignmentCenter;
        text.foregroundColor = [[UIColor whiteColor] CGColor];

       [progressLayer addSublayer:text];
    }
CAShapeLayer*progressLayer=[[CAShapeLayer alloc]init];
[progressLayer设置路径:bezierPath.CGPath];
CATextLayer*文本=[CATextLayer新建];

对于(int i=1;i这将只是用椭圆来掩盖文本。我不相信它会将文本包装成椭圆的形状,如果这是你想要的形状的话。@ZevEisenberg这个问题很模糊。但是,将两个单词包装成一个形状似乎没有我所做的那么有意义。你知道,你可以只提供你自己的答案,而不需要如果你觉得别人的评论不正确,我会尽量避免使用drawRect@heneriklarsen因为?你能提供完整的例子吗