Отображение текста в двух столбцах с использованием UITextview:
//Declare global variables.
NSLayoutManager *_layoutManager;
NSTextStorage *_textStorage;
UIScrollView *scrollView;
//Create a scrollview and add it on the main view.
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 10, screenWidth-20, screenHeight-20)];
[self.view addSubview:scrollView];
//Get the file url path of .txt file.
NSURL *contentURL = [[NSBundle mainBundle] URLForResource:@"introduction" withExtension:@"txt"];
//Create text storage :
_textStorage = [[NSTextStorage alloc] initWithFileURL:contentURL
options:nil
documentAttributes:NULL
error:NULL];
//Create a layout manager and add it to textstorage
_layoutManager = [[NSLayoutManager alloc] init];
[_textStorage addLayoutManager:_layoutManager];
//then call the method
[self layoutTextContainers];
определение метода:
-(void)layoutTextContainers{
NSUInteger lastRenderedGlyph = 0;
CGFloat currentXOffset = 0;
while (lastRenderedGlyph < _layoutManager.numberOfGlyphs) {
CGRect textViewFrame = CGRectMake(currentXOffset, 10,
CGRectGetWidth(scrollView.bounds) / 2,
CGRectGetHeight(scrollView.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;
[introTextScrollView 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]);
}
// Need to update the scrollView size
CGSize contentSize = CGSizeMake(currentXOffset, CGRectGetHeight(introTextScrollView.bounds));
introTextScrollView.contentSize = contentSize;
}