在iOS7中如何将UITextView的内容放置在平行四边形形状内部?

3
我有一系列的uitextviews在一个分页scrollview中。我希望每个页面内的文本可以在一个倾斜的矩形 (平行四边形) 中流动。附上图片 (我希望文本在白色轮廓线内流动)。
下方是相关代码。
for (int i = 0; i < imageArray.count; i++) {

        CGRect framee;
        framee.origin.x = self.uis_scrollView.frame.size.width * i;
        framee.origin.y = 0;
        framee.size = self.uis_scrollView.frame.size;

        UITextView *newTextView = [[UITextView alloc] initWithFrame:framee];

        UIBezierPath* rectanglePath = [UIBezierPath bezierPath];
        [rectanglePath moveToPoint: CGPointMake(652, 687)];
        [rectanglePath addLineToPoint: CGPointMake(680, 687)];
        [rectanglePath addLineToPoint: CGPointMake(746, 541)];
        [rectanglePath addLineToPoint: CGPointMake(718, 541)];
        [rectanglePath addLineToPoint: CGPointMake(652, 687)];
        [rectanglePath closePath];
        UIBezierPath * imgRect = rectanglePath;
        newTextView.textContainer.exclusionPaths = @[imgRect];

        [newTextView setBackgroundColor:[UIColor clearColor]];
        [newTextView setTextColor:[UIColor whiteColor]];
        [newTextView setFont:[UIFont systemFontOfSize:16]];
        newTextView.text = [imageArray objectAtIndex:i];
        newTextView.userInteractionEnabled = NO;
        [_uis_scrollView addSubview:newTextView];
    }

也许只是用来流动吗?也许我需要在右侧“绘制”一个倾斜的矩形,以限制内容?
1个回答

6

排除路径的目的确实只是为了使文本绕过它流动。您应该创建一个或多个排除路径,覆盖您不想看到任何文本的三角形区域。请记住,这些路径必须UITextView的坐标系中。如果我有一个大小为{320,218}的文本视图,并且我想要剪切右侧的三角形部分,则代码应如下所示:

UIBezierPath* path = [UIBezierPath bezierPath];
[path moveToPoint: CGPointMake(320, 0)];
[path addLineToPoint: CGPointMake(320, 218)];
[path addLineToPoint: CGPointMake(210, 0)];
[path closePath];
self.textView.textContainer.exclusionPaths = @[path];

然后的结果看起来像这样: 除去三角形文本视图的截图 您的问题在于,您想要从父视图坐标系中减去一个矩形,以使每个文本视图。据我所知,没有方法可以创建具有CGRect的任意UIBezierPath交集,因此您将不得不为每个textView手动计算几个UIBezierPath三角形的坐标。
为了简单起见,您可以只计算三角形排除路径的斜边与textView矩形之间的交点。这是您屏幕的一张风格化图片,向左旋转了90度: 几何

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接