NSTextContainer与非规则形状的例子?

您好我正在与TextKit的新的TextKit API,我试图产生一个不规则形状的UITextView 。 到目前为止,我在一个视图控制器中:

 -(void) loadView { self.view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,548)]; NSTextStorage *textStorage = [[NSTextStorage alloc] init]; NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; [textStorage addLayoutManager: layoutManager]; BaseTextContainer *textContainer = [[BaseTextContainer alloc] initWithSize:CGSizeMake(100, 100)]; [layoutManager addTextContainer: textContainer]; BaseTextView *textView = [[BaseTextView alloc] initWithFrame:CGRectMake(110,124, 100, 100) textContainer:textContainer]; textView.backgroundColor = [UIColor blueColor]; textView.editable = YES; [self.view addSubview:textView]; } 

然后在我的子类NSTextContainer ,我想有一个mutablePath作为文本容器的形状绘制,但不知道如何做到这一点。 我有:

 - (BOOL) isSimpleRectangularTextContainer { return NO; } - (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { NSLog(@"TEST"); CGContextRef context = ctx; CGSize layerSize = layer.frame.size; CGAffineTransform transform = CGAffineTransformMakeScale(layerSize.width / self.initialSize.width, layerSize.height / self.initialSize.height); CGMutablePathRef newGraphicMutablePath = CGPathCreateMutableCopyByTransformingPath(self.mutablePath, &transform); CGContextAddPath(context, newGraphicMutablePath); CGPathRelease(newGraphicMutablePath); CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); CGContextDrawPath(context, kCGPathFill); } 

只是有点困惑如何得到这个工作。 我在NSTextContainer任何地方都NSTextContainer一个不规则形状的例子。

不需要构buildText Kit堆栈的所有代码,因为您不修改堆栈的体系结构。 从一个普通的UITextView开始 – 让我们说它是self.textView – 然后分配一个或多个UIBezierPath对象到它的排除path:

 self.tv.textContainer.exclusionPaths = myArrayOfBezierPaths; 

这些path是排除path,因此对于椭圆,您将需要创build四条path,每条path描述文本容器的一个angular落。

或者,您可以自己构build文本工具包堆栈,以便插入自己的文本容器子类,并通过重写lineFragmentForProposedRect:来修改允许文本的lineFragmentForProposedRect: ,可能类似于我在此处所做的: https : //github.com/ mattneub /编程-IOS-书本实例/斑点/主/ bk2ch10p537exclusionPath2 / ch23p813textKitShapes / MyTextContainer.swift

一些实验:

在这里输入图像说明

在这里输入图像说明