Premature line wrapping in NSTextView when tabs are used

萝らか妹 提交于 2020-01-20 08:44:05

问题


I have a strange problem with NSTextView linewrapping after the 51st column if I enter a line of tabs. This only happens with tabs, not with any other character, which wrap correctly at the edge of the text view, not after the 51st character.

This is easy to repeat. Create a blank project in XCode with a single window and just one NSTextView. The only non-default settings are that I have removed constraints, and used the old style autosize to autosize the textview so that it fills the window. I have written no code. Now run the application, open up the window so that is much wider than 51 characters, hold down the tab key and it will wrap early.

Thanks in advance.


回答1:


The issue here is that NSTextView has a default NSMutableParagraphStyle object which has a list of attributes such as line wrapping, tab stops, margins, etc... You can see this by going to the Format menu, text subview, and select the "Show Ruler" menu. (You get this menu for free with any NSTextView).

Once you show the ruler you will see all of your tab stops and this will explain why your tabs are wrapping once you reach the last tab stop.

So the solution you need is to create an array of tabs that you want for your paragraph style object and then set that to be the style for the NSTextView.

Here is a method to create tabs. In this example, it will create 5 left aligned tabs, each 1.5 inches apart:

-(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;
}

Then you invoke it before you add any text to your NSTextView in order to set the default paragraph style with those tab stops in it:

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

You can find a additional informatio here, if you want a deeper tutorial:

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




回答2:


I'm sharing my experience since I recently had kind of the same type of problem(s) - when pressing tabs, cursor jumps to the next line after about 10-12 tabs - when there are multiple lines of text, when pressing tabs the whole paragraph turns into bulleted lines

I used the above method by "Ed Fernandez" and could only resolve the problem when there is no text in the NSTextView initially but when existing saved text is loaded it had above problems

For this I tried the below code from below link (It really worked and solved the both problems) http://www.cocoabuilder.com/archive/cocoa/159692-nstextview-and-ruler-tab-settings.html

You don't need to call "release" if you are using automatic reference counting.

- (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])];
}

This tab limit is there due to the "Ruler" concept where it's limited to about 12 tabstops. You can see the Ruler by calling

[editorTextView setRulerVisible:YES];


来源:https://stackoverflow.com/questions/14278119/premature-line-wrapping-in-nstextview-when-tabs-are-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!