嗨,我正在使用新的TextKit iOS7 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的例子。
发布于 2014-04-25 15:42:22
不需要构建文本工具包堆栈的所有代码,因为您没有修改堆栈的体系结构。从一个普通的UITextView开始--假设它是self.textView --然后将一个或多个UIBezierPath对象分配给它的排除路径:
self.tv.textContainer.exclusionPaths = myArrayOfBezierPaths;这些路径是排除路径,因此对于椭圆,您需要创建四条路径,每条路径都描述文本容器的一个角。
或者,您可以自己构建文本工具包堆栈,以便插入您自己的文本容器子类,并通过重写lineFragmentForProposedRect:修改文本可以进入的位置,这可能与我在这里所做的类似:https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch10p537exclusionPath2/ch23p813textKitShapes/MyTextContainer.swift。
一些实验:


https://stackoverflow.com/questions/23285721
复制相似问题