我必须在iOS 6和iOS 7上制作这样的东西:
但我不知道该怎么做。 你能救我吗?
答案 0 :(得分:3)
要使用CoreText在图像周围绘制文本,您需要创建CTFramesetterRef,
然后通过调用CTFrameRef
创建一个带有指定框架形状的路径的CTFramesetterCreateFrame
。
在UIView
的左上角绘制图像的示例代码以及图像周围的一些文字:
@implementation MyView
- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:@"YourArrowImage"];
// create the transform to flip the path and text drawing
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, rect.size.height);
transform = CGAffineTransformScale(transform, 1., -1.);
// create a path that's exclude the image rect { .origin = CGPointZero, .size = [image size] } from the rect
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, &transform, [image size].width, 0);
CGPathAddLineToPoint(path, &transform, rect.size.width, 0);
CGPathAddLineToPoint(path, &transform, rect.size.width, rect.size.height);
CGPathAddLineToPoint(path, &transform, 0, rect.size.height);
CGPathAddLineToPoint(path, &transform, 0, [image size].height);
CGPathAddLineToPoint(path, &transform, [image size].width, [image size].height);
CGPathCloseSubpath(path);
// setup your text with the desired attributes
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus."
attributes:@{ NSFontAttributeName :[UIFont systemFontOfSize:24.],
NSForegroundColorAttributeName: [UIColor grayColor] }];
// create CoreText framesetter and the text frame to draw
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);
[attributedString release];
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,0), path, NULL);
CFRelease(path);
CFRelease(framesetter);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// save graphics state
CGContextSaveGState(ctx);
// apply the transfom to draw the text
CGContextConcatCTM(ctx, transform);
// draw the text frame
CTFrameDraw(frame, ctx);
// restore graphics state
CGContextRestoreGState(ctx);
CFRelease(frame);
// draw the image
[image drawAtPoint:CGPointMake(0,0)];
}
@end
答案 1 :(得分:1)
我不能建议你如何为iOS6实现它。但在iOS7中,它很容易用
实现UIBezierPath* exclusionPath = ... create exclusion path here
_textView.textContainer.exclusionPaths = @[exclusionPath];
非常好的教程 - http://www.raywenderlich.com/50151/text-kit-tutorial