How to limit the content in UITextView in ios

前端 未结 7 2272
迷失自我
迷失自我 2021-02-08 11:56

I want to load long text in TextViews of different Views. The text should be divided to pages when it reaches end of the textviews. And the next textview must start with the con

7条回答
  •  遇见更好的自我
    2021-02-08 12:47

    Here are two element:

    A good explanation/sample for multi column with ios 7 text kit:

    https://github.com/ShinobiControls/iOS7-day-by-day/blob/master/21-multi-column-textkit/21-multi-column-textkit.md

    Based on that sample and the following question:

    How do I locate the CGRect for a substring of text in a UILabel?

    You can try something like that:

    - (void)layoutTextContainers
    {
        NSUInteger lastRenderedGlyph = 0;
        CGFloat currentXOffset = 0;
        while (lastRenderedGlyph < _layoutManager.numberOfGlyphs) {
            CGRect textViewFrame = CGRectMake(currentXOffset, 10,
                                              CGRectGetWidth(self.view.bounds) / 2,
                                              CGRectGetHeight(self.view.bounds) - 20);
            CGSize columnSize = CGSizeMake(CGRectGetWidth(textViewFrame) - 20,
                                           CGRectGetHeight(textViewFrame) - 10);
    
            NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:columnSize];
            [_layoutManager addTextContainer:textContainer];
    
            // And a text view to render it
            UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
                                                       textContainer:textContainer];
            textView.scrollEnabled = NO;
            [self.scrollView addSubview:textView];
    
    
    
            // Increase the current offset
            currentXOffset += CGRectGetWidth(textViewFrame);
    
            // And find the index of the glyph we've just rendered
            lastRenderedGlyph = NSMaxRange([_layoutManager glyphRangeForTextContainer:textContainer]);
    
            NSLog(@"Last rendered glyph %i",lastRenderedGlyph);
    
            NSLog(@"Textview length %i",textView.text.length);
    
            NSRange range = {lastRenderedGlyph-1, lastRenderedGlyph};
    
            NSRange glyphRange;
    
            // Convert the range for glyphs.
            [_layoutManager characterRangeForGlyphRange:range actualGlyphRange:&glyphRange];
    
            NSLog(@"Glyph rect %@",NSStringFromCGRect([_layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer]));
        }
    
        // Need to update the scrollView size
        CGSize contentSize = CGSizeMake(currentXOffset, CGRectGetHeight(self.scrollView.bounds));
        self.scrollView.contentSize = contentSize;
    }
    

    output is:

     Newspaper[89217:a0b] Last rendered glyph 711
     Newspaper[89217:a0b] Textview length 2585
     Newspaper[89217:a0b] Glyph rect {{121.556, 515.3761}, {13.444, 14.315979}}
     Newspaper[89217:a0b] Last rendered glyph 1441
     Newspaper[89217:a0b] Textview length 2585
     Newspaper[89217:a0b] Glyph rect {{129.15199, 515.3761}, {5.8480072, 14.315979}}
     Newspaper[89217:a0b] Last rendered glyph 2155
     Newspaper[89217:a0b] Textview length 2585
     Newspaper[89217:a0b] Glyph rect {{111.80001, 515.3761}, {23.199989, 14.315979}}
     Newspaper[89217:a0b] Last rendered glyph 2585
     Newspaper[89217:a0b] Textview length 2585
     Newspaper[89217:a0b] Glyph rect {{92.720001, 329.26801}, {3.552002, 14.31601}}
    

    Glyph Rect doesn't looks good (i'm interested to see it working correctly) but you get the latest glyph displayed.

    Hope it helps!

提交回复
热议问题