Adding exclusion paths to multiple text views

2019-05-17 20:56发布

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]);
}

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?

2条回答
爷的心禁止访问
2楼-- · 2019-05-17 21:11

This is Very easy and helpful thing for ImageWrapping in RichText. but One should keep thing in Mind. a day ago I tried this one and found same code working differently for iOS 7.0 and iOS 7.1

The Blinking Cursor of TextView appears at different place until there is no text in UITextView.

I wrote this code.

UIBezierPath* exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 38, 21)];
_txtViewMessage.textContainer.exclusionPaths  = @[exclusionPath];

and Found Following result in iOS 7.1 and iOS 7.0

Left One:iOS 7.0  & Right One : iOS 7.1

查看更多
疯言疯语
3楼-- · 2019-05-17 21:30

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];

rather than:

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

which causes the app to become unresponsive.

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.

查看更多
登录 后发表回答