我想在两列中显示某个文本,就像这样:
================
line1 line4 ||
line2 line5 ||
line3 line6 ||
------------ ||
prev next ||
================
我不希望文本有垂直或水平滚动只是适合在区域内。
我不知道使用哪个视图或布局-因为我是IOS开发的新手-
谢谢
如果你有一些html技能,你可以使用UIWebView以任何你可以在标记中实现的方式来显示你的文本。
页面http://www.cocoanetics.com/2011/01/befriending-core-text/解释了如何使用CoreText框架和NSAttributedString显示两列文本。
使用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;
}
我认为你可以添加2个相同大小的uitextview。在左边,你可以写第1、2和3行,在其他的第4、5和6行