3

新工作的第一天,我被要求在 iPad 应用程序上提供帮助。他们要求我沿着弯曲的路径绘制一个 NSString(曲线有点像 S 形)。

如果不循环遍历字符串的字母并在旋转和移动它的框架后单独绘制每个字母,我不确定如何做到这一点。有没有更有效的方法来做到这一点?

这是我到目前为止所拥有的:

-(void)doCurveString:(NSString *)stringToCurve{

    UIGraphicsBeginImageContext(CGSizeMake(768, 1024));


    NSArray *arrayFullOfCGRects = [NSArray arrayWithObjects:
                                   [NSValue valueWithCGRect:CGRectMake(300, 500, 20, 20)],
                                   [NSValue valueWithCGRect:CGRectMake(315, 510, 20, 20)],
                                   [NSValue valueWithCGRect:CGRectMake(330, 500, 20, 20)],
                                   [NSValue valueWithCGRect:CGRectMake(345, 510, 20, 20)],
                                   [NSValue valueWithCGRect:CGRectMake(360, 500, 20, 20)],
                                   [NSValue valueWithCGRect:CGRectMake(375, 510, 20, 20)],
                                   [NSValue valueWithCGRect:CGRectMake(390, 500, 20, 20)],
                                   [NSValue valueWithCGRect:CGRectMake(405, 510, 20, 20)],
                                   [NSValue valueWithCGRect:CGRectMake(420, 500, 20, 20)],
                                   [NSValue valueWithCGRect:CGRectMake(435, 510, 20, 20)],
                                   nil];

    NSArray *arrayFullOfRotateTransforms = [NSArray arrayWithObjects:
                                            [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/4)],
                                            [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/2)],
                                            [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/1)],
                                            [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/4)],
                                            [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/2)],
                                            [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/1)],
                                            [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/3)],
                                            [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/5)],
                                            [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/6)],
                                            [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/1)],
                                            nil];

    NSDictionary *drawDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                    arrayFullOfCGRects, @"frames",
                                    arrayFullOfRotateTransforms, @"rotations",
                                    nil];

    for(NSInteger charIndex=0; charIndex<stringToCurve.length; charIndex++){

        //Create a label and set it's frame coordinates
        UILabel *labelToDraw = [[UILabel alloc] initWithFrame:[[[drawDictionary objectForKey:@"frames"] objectAtIndex:charIndex] CGRectValue]];

        //Rotate the letter with the transform stored in the array
        labelToDraw.transform = [[[drawDictionary objectForKey:@"rotations"] objectAtIndex:charIndex] CGAffineTransformValue];

        //Set the label text to the letter at the correct index
        labelToDraw.text = [NSString stringWithFormat:@"%C", [stringToCurve characterAtIndex:charIndex]];

        //Make sure the background colour is transparent
        labelToDraw.backgroundColor = [UIColor clearColor];

        //Draw it to the view controller view
        [self.view addSubview:labelToDraw];

    }

    UIGraphicsEndImageContext();

肯定有更好的方法吗?我不知道/找不到的 drawTextOnPath:someCGPath 方法?

4

0 回答 0