核心文本与iOS上的不对称形状

我想写一个可以采取任何形状的path文本。 现在我可以在其中绘制翻转的文本:

NSArray *coordinates = ...; CGMutablePathRef pathRef = [self objectPathGivenCoordinates:coordinates]; ... // Draw the shapes // Now draw the text CGContextSetTextMatrix(context, CGAffineTransformIdentity); NSAttributedString* attString = [[NSAttributedString alloc] initWithString:@"0,1,2..."]; CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attString); CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, [attString length]), pathRef, NULL); CTFrameDraw(frame, context); 

这产生了这个:

在不对称形状中翻转文本

我知道iOS有一个翻转的坐标系统。 大多数解决scheme在绘制前添加这两行:

 CGContextTranslateCTM(context, 0, rect.size.height); CGContextScaleCTM(context, 1.0, -1.0); 

但是,这产生了:

文本现在是左右右侧,但超出了路径边界

那么为什么这种技术不工作? 在我看到的每一个例子中,当文本以对称或剪裁的方式呈现时,或在OS X上使用时,都会使用这种技术。如果我们以对称形状绘制文本,则调用CGContextTranslateCTM()CGContextScaleCTM()合理。 对CGContextTranslateCTM(context, 0, rect.size.height)的调用将第一个字母的原点移动到顶部。 对CGContextScaleCTM(context, 1, -1)的调用反转Y的方向。如果我们在对称形状上反转Y的方向,则它不会改变。 可悲的是,我的形状不对称。

这是我已经考虑但放弃了

  • 在相同的尺度上绘制形状和数字(颠倒),然后翻转它后面的UIImageView (1)
  • 不要打电话来改变比例或翻译CTM。 用CGContextSetTextMatrix()翻转文本matrix,使文本从左到右,从下到上,正面朝上绘制。 确定形状的string确定后,形状中剩余多less个字符。 反转的话(现在它将从上到下,从右到左,右侧向上)。 为每个额外字符添加空格。 现在对于每一个CTLine ,都会以某种方式再次颠倒单词(现在它将从上到下,从左到右,从右到上)(2)

为什么我放弃了他们

(1)我不能翻转我的图像。 他们总是需要正确的一面

(2)这可能是一个简单的解决scheme别人知道的矫枉过正

我确定了一个解决scheme

 // Flip the coordinates so that the object is drawn as if it were mirrored along the x-axis // coordinates = @[CGPointMake(1, 2), CGPointMake(9, 17), CGPointMake(41, 3), ...] NSArray *flippedCoordinates = [coordinates map:^id(id object) { CGPoint value; [object getValue:&value]; CGPoint point = value; point.y = point.y * (-1) + rect.size.height; return [NSValue value:&point withObjCType:@encode(CGPoint)]; }]; // Generate a CGPathRef using the new coordinates CGMutablePathRef pathRef = CGPathCreateMutable(); CGPathMoveToPoint(pathRef, NULL, [[flippedCoordinates objectAtIndex:0] CGPointValue].x + .5 [[flippedCoordinates objectAtIndex:0] CGPointValue].y + .5); for(NSValue *arrayValue in flippedCoordinates) { CGPoint point = [arrayValue CGPointValue]; CGPathAddLineToPoint(pathRef, NULL, point.x, point.y); } CGPathCloseSubpath(pathRef); // Draw the object // ... // Draw the text as if it were on OS X CGContextSetTextMatrix(context, CGAffineTransformIdentity); NSAttributedString* attString = [[NSAttributedString alloc] initWithString:@"0,1,2,..." attributes:@{(NSString *)kCTForegroundColorAttributeName : textColor}]; CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attString); CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, [attString length]), pathRef, NULL); CTFrameDraw(frame, context); CFRelease(frame); CFRelease(frameSetter); CFRelease(pathRef); // And, for the magic touch, flip the whole view AFTER everything is drawn [self.layer setAffineTransform:CGAffineTransformMakeScale(1, -1)]; 

现在一切都从上到下,从左到右,右侧向上

核心文本在iOS中的不对称形状上正确布局