2013-01-11 13 views
0

如果輸入一行製表符,我在第51列之後出現NSTextView行纏繞的奇怪問題。這隻發生在製表符上,而不是任何其他字符,它們在文本視圖的邊緣正確包裝,而不是在第51個字符之後。使用製表符時NSTextView中的過早換行

這很容易重複。使用單個窗口和一個NSTextView在XCode中創建一個空白項目。唯一的非默認設置是我已經刪除了約束,並使用舊式autosize來自動調整textview,使其填充窗口。我沒有寫任何代碼。現在運行該應用程序,打開窗口,使其比51個字符寬得多,按住Tab鍵並提前打包。

在此先感謝。

回答

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

0

我分享我的experi因爲我最近遇到了類似的問題 - 按下標籤時,光標跳到下一行約10-12個標籤 - 當有多行文本時,按下標籤時整個段落轉動成符號線

我所用「埃德·費爾南德斯」上面的方法,當有在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]; 
相關問題