用UIBezierPath单行绘制字母(文本)

我正在使用这个和这个参考。 我是新的使用UIBezierPath,我想绘制字符(字母)代码我使用的path如下。

CGMutablePathRef letters = CGPathCreateMutable(); CTFontRef font = CTFontCreateWithName(CFSTR("Courier New"), 200.0f, NULL); NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:CFBridgingRelease(font), kCTFontAttributeName,nil]; NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"B" attributes:attrs]; CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString); CFArrayRef runArray = CTLineGetGlyphRuns(line); // for each RUN for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++) { // Get FONT for this run CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex); CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName); // for each GLYPH in run for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++) { // get Glyph & Glyph-data CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1); CGGlyph glyph; CGPoint position; CTRunGetGlyphs(run, thisGlyphRange, &glyph); CTRunGetPositions(run, thisGlyphRange, &position); // Get PATH of outline { CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL); CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y); CGPathAddPath(letters, &t, letter); CGPathRelease(letter); } } } CFRelease(line); UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointZero]; [path appendPath:[UIBezierPath bezierPathWithCGPath:letters]]; CGPathRelease(letters); CFRelease(font); NSLog(@"==> %@",path); // Creates layer CAShapeLayer *pathLayer = [CAShapeLayer layer]; pathLayer.frame = drawingView.bounds; pathLayer.bounds = CGPathGetBoundingBox(path.CGPath); //pathLayer.backgroundColor = [[UIColor yellowColor] CGColor]; pathLayer.geometryFlipped = NO; pathLayer.path = path.CGPath; pathLayer.strokeColor = [[UIColor blackColor] CGColor]; pathLayer.fillColor = nil; pathLayer.lineWidth = 3.0f; pathLayer.lineJoin = kCALineJoinBevel; [drawingView.layer addSublayer:pathLayer]; self.pathLayer = pathLayer; // Add pointer UIImage *penImage = [UIImage imageNamed:@"hand.png"]; CALayer *penLayer = [CALayer layer]; penLayer.contents = (id)penImage.CGImage; penLayer.anchorPoint = CGPointMake(0.35f, 0.98f); penLayer.frame = CGRectMake(0.0f, 0.0f, penImage.size.width, penImage.size.height); [pathLayer addSublayer:penLayer]; self.penLayer = penLayer; CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; pathAnimation.duration = 5.0; pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; pathAnimation.toValue = [NSNumber numberWithFloat:1.0f]; [self.pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"]; CAKeyframeAnimation *penAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; penAnimation.duration = 5.0; penAnimation.path = self.pathLayer.path; penAnimation.calculationMode = kCAAnimationPaced; penAnimation.delegate = self; [self.penLayer addAnimation:penAnimation forKey:@"position"]; 

这个代码像下面的图片一样以双线的forms绘制后者 在这里输入图像说明

相反,我想绘制像下面的图像单行的后者 在这里输入图像说明

而手应在字体的中心绘制线条而不是边框​​。

Interesting Posts