繁体   English   中英

具有非规则形状的NSTextContainer示例?

[英]Example of NSTextContainer with non regular shape?

嗨,我正在使用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任何示例。

不需要构建Text Kit堆栈的所有代码,因为您没有修改堆栈的体系结构。 只需从普通的UITextView开始 - 假设它是self.textView - 然后将一个或多个UIBezierPath对象分配给它的排除路径:

self.tv.textContainer.exclusionPaths = myArrayOfBezierPaths;

这些路径是排除路径,因此对于椭圆,您将需要创建四条路径,每条路径描述文本容器的一角。

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

一些实验:

在此输入图像描述

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM