ここでの問題は、NSTextViewにデフォルトのNSMutableParagraphStyleオブジェクトがあり、行の折り返し、タブストップ、マージンなどの属性のリストがあることです。これは、[フォーマット]メニュー、テキストサブビューに移動し、[表示]を選択することで確認できます。ルーラー」メニュー。(このメニューは、NSTextViewで無料で入手できます)。
ルーラーを表示すると、すべてのタブストップが表示されます。これにより、最後のタブストップに到達したときにタブが折り返される理由が説明されます。
したがって、必要な解決策は、段落スタイルオブジェクトに必要なタブの配列を作成し、それをNSTextViewのスタイルに設定することです。
タブを作成する方法は次のとおりです。この例では、それぞれ1.5インチ離れた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