如何在iPad上的两列中显示文本

时间:2011-11-01 05:29:29

标签: objective-c ios cocoa-touch ipad uitextview

我想在两列中显示某个文本,如下所示:

================
line1  line4  ||
line2  line5  ||
line3  line6  ||
------------  ||
prev  next    ||

=====

我不希望文本在该区域内具有垂直或水平滚动。

我不知道要使用哪种视图或布局 - 因为我是IOS开发的新手 -

感谢

4 个答案:

答案 0 :(得分:4)

如果您具备html技能,可以使用UIWebView以任何可以在标记中实现的方式显示文本。

答案 1 :(得分:1)

页面http://www.cocoanetics.com/2011/01/befriending-core-text/解释了如何使用CoreText框架和NSAttributedString显示两列文本。我希望它可以帮助你。

答案 2 :(得分:0)

使用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;
    }

答案 3 :(得分:-1)

我认为你可以添加2个相同大小的uitextviews。在左边一个,你可以写第1,2和3行,在其他4,5和6