【问题标题】:Premature line wrapping in NSTextView when tabs are used使用选项卡时在 NSTextView 中过早换行
【发布时间】:2013-01-11 12:22:37
【问题描述】:

如果我输入一行制表符,我在第 51 列之后的 NSTextView 换行有一个奇怪的问题。这只发生在制表符上,而不是任何其他字符,它们在文本视图的边缘正确换行,而不是在第 51 个字符之后。

这很容易重复。在 XCode 中创建一个带有单个窗口和一个 NSTextView 的空白项目。唯一的非默认设置是我删除了约束,并使用旧样式自动调整大小来自动调整文本视图的大小,以便它填满窗口。我没有写任何代码。现在运行应用程序,打开大于 51 个字符的窗口,按住 Tab 键,它会提前换行。

提前致谢。

【问题讨论】:

    标签: macos cocoa nstextview


    【解决方案1】:

    这里的问题是 NSTextView 有一个默认的 NSMutableParagraphStyle 对象,它有一个属性列表,例如换行、制表位、边距等...您可以通过转到“格式”菜单、文本子视图并选择“显示标尺”菜单。 (您可以使用任何 NSTextView 免费获得此菜单)。

    显示标尺后,您将看到所有制表位,这将解释为什么您的制表位在到达最后一个制表位时会换行。

    因此,您需要的解决方案是为段落样式对象创建一个选项卡数组,然后将其设置为 NSTextView 的样式。

    这是一种创建标签的方法。在本例中,它将创建 5 个左对齐的标签,每个标签相距 1.5 英寸:

    -(NSMutableAttributedString *) textViewTabFormatter:(NSString *)aString
    {
        float columnWidthInInches = 1.5f;
        float pointsPerInch = 72.0f;
    
        NSMutableArray * tabArray = [NSMutableArray arrayWithCapacity:5];
    
        for(NSInteger tabCounter = 0; tabCounter < 5; tabCounter++)
        {
            NSTextTab * aTab = [[NSTextTab alloc] initWithType:NSLeftTabStopType location:(tabCounter * columnWidthInInches * pointsPerInch)];
            [tabArray addObject:aTab];
        }
    
        NSMutableParagraphStyle * aMutableParagraphStyle = [[NSParagraphStyle defaultParagraphStyle]mutableCopy];
        [aMutableParagraphStyle setTabStops:tabArray];
    
        NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:aString];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:aMutableParagraphStyle range:NSMakeRange(0,[aString length])];
    
        return attributedString;
    }
    

    然后在向 NSTextView 添加任何文本之前调用它,以便设置带有这些制表位的默认段落样式:

    [[mainTextView textStorage] setAttributedString:[self textViewTabFormatter:@" "]];
    

    如果您想要更深入的教程,您可以在此处找到更多信息:

    http://www.mactech.com/articles/mactech/Vol.19/19.08/NSParagraphStyle/index.html

    【讨论】:

      【解决方案2】:

      我正在分享我的经验,因为我最近遇到了相同类型的问题 - 按下制表符时,光标在大约 10-12 个制表符后跳转到下一行 - 当有多行文本时,当按下制表符时,整个段落会变成项目符号行

      我使用“Ed Fernandez”的上述方法,只能在最初在 NSTextView 中没有文本但加载现有保存的文本时出现上述问题时才能解决问题

      为此,我尝试了以下链接中的以下代码(它确实有效并解决了这两个问题) http://www.cocoabuilder.com/archive/cocoa/159692-nstextview-and-ruler-tab-settings.html

      如果您使用自动引用计数,则无需调用“释放”。

      - (IBAction)formatTextView:(NSTextView *)editorTextView
      {
         int cnt;
         int numStops = 20;
         int tabInterval = 40;
         NSTextTab *tabStop;
      
         NSMutableDictionary *attrs = [[NSMutableDictionary alloc] init];
         //attributes for attributed String of TextView
      
         NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
      
         // This first clears all tab stops, then adds tab stops, at desired intervals...
         [paraStyle setTabStops:[NSArray array]];
         for (cnt = 1; cnt <= numStops; cnt++) {
            tabStop = [[NSTextTab alloc] initWithType:NSLeftTabStopType location: tabInterval * (cnt)];
            [paraStyle addTabStop:tabStop];
         }
      
         [attrs setObject:paraStyle forKey:NSParagraphStyleAttributeName];
      
         [[editorTextView textStorage] addAttributes:attrs range:NSMakeRange(0, [[[editorTextView textStorage] string] length])];
      }
      

      由于“标尺”概念限制了大约 12 个制表位,因此存在此标签限制。你可以通过调用看到标尺

      [editorTextView setRulerVisible:YES];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-25
        • 1970-01-01
        • 2011-01-18
        • 1970-01-01
        • 2014-08-31
        • 1970-01-01
        • 1970-01-01
        • 2013-07-21
        相关资源
        最近更新 更多