【问题标题】:How to display text in two columns on an iPad如何在 iPad 上分两列显示文本
【发布时间】:2011-11-01 05:29:28
【问题描述】:

我想像这样在两列中显示某个文本:

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

=================

我不希望文本的垂直或水平滚动刚好适合该区域。

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

谢谢

【问题讨论】:

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


【解决方案1】:

如果你有一些 html 方面的技能,你可以使用 UIWebView 以任何你可以在标记中实现的方式来显示你的文本。

【讨论】:

  • @Павел Оганесян 是的,我有一些 html 技能,但我不知道如何让文本先填充左侧固定大小列,然后开始填充右侧固定大小列。谢谢
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 这就是我已经尝试做的,但我面临一个问题。问题是如何知道文本填充左侧 UITextView 以开始填充另一个。我如何知道要设置为第一个 UITextView 的确切行数(或单词)
      • 我在询问之前已经阅读了这个主题。它对我的情况没有用。因为它们是两个不同的情况。感谢您的努力
      • 对不起,我帮不了你更多,但如果我找到任何东西,我会帮助你
      猜你喜欢
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 2022-11-12
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      相关资源
      最近更新 更多