将排除路径添加到多个文本视图 [英] Adding exclusion paths to multiple text views

查看:133
本文介绍了将排除路径添加到多个文本视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将多个排除路径添加到在UIScrollView中连续布置的一系列UITextView中,如下所示:

I'm trying to add multiple exclusion paths to a series of UITextViews laid out successively in a UIScrollView, like so:

while (lastRenderedGlyph < self.manager.numberOfGlyphs) {
    CGRect textViewFrame = CGRectMake(currentXOffset, 10,
                                      width / 2,
                                      height - 20);
    CGSize columnSize = CGSizeMake(CGRectGetWidth(textViewFrame) - 20,
                                   CGRectGetHeight(textViewFrame) - 10);

    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:columnSize];
    [self.manager addTextContainer:textContainer];

    UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
                                               textContainer:textContainer];
    textView.scrollEnabled = NO;
    textView.editable = NO;
    textView.dataDetectorTypes = UIDataDetectorTypeAll;
    textView.delegate = self;
    textView.selectable = YES;

    UIImageView *goat = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"goat"]];
    [goat setContentMode:UIViewContentModeScaleAspectFit];
    goat.frame = CGRectMake(0.0, 0.0, 50.0, 50.0);

    [textView addSubview:goat];

    [self.scrollView addSubview:textView];

    textView.textContainer.exclusionPaths = @[[UIBezierPath bezierPathWithRect:CGRectMake(0.0, 0.0, 50.0, 50.0)]];

    currentXOffset += CGRectGetWidth(textViewFrame);

    lastRenderedGlyph = NSMaxRange([self.manager glyphRangeForTextContainer:textContainer]);
}

但是,这导致应用程序冻结,并且我已将问题追究到每个NSTextContainer上排除路径的设置.例如,如果我未设置排除路径,则可以正常运行.重要的是,如果我仅在第一个NSTextContainer上设置了排除路径,则一切正常,但任何大于1的东西都将冻结.我在做什么错,还是这是个错误?

However, this causes the app to freeze up, and I've traced the issue to the setting of the exclusion path on each NSTextContainer. For example, if I set no exclusion paths, it works fine. Importantly, if I only set the exclusion path on the first NSTextContainer, then everything works just fine - but anything above one, and the app freezes. What am I doing wrong, or is this a bug?

推荐答案

我回想起来很明显,但是我得出的解决方案是在分配容器之后立即为每个新的NSTextContainer添加排除路径,但是<在执行其他任何操作之前先.即

I guess in retrospect this is obvious, but the solution I arrived at is to add exclusion paths to each new NSTextContainer right after you allocate the container but before doing anything else. i.e.

NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:columnSize];
textContainer.exclusionPaths = @[exclusionPath];
[manager addTextContainer: textContainer];

而不是:

NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:columnSize];
[manager addTextContainer: textContainer];
textContainer.exclusionPaths = @[exclusionPath];

这会导致应用无响应.

简单易懂.这仅适用于多列,多页面的布局,其中您将创建多个文本容器和视图,并分解字形以使其合适.如果您只向一个视图添加一个排除路径,则似乎可以在任何位置添加排除路径.

Simple when you know how. This only applies to multi-column, multi-page layouts where you're creating multiple text containers and views and breaking up the glyphs to fit properly. If you're only adding one exclusion path to one view, seems like you can add the exclusion path anywhere.

这篇关于将排除路径添加到多个文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆