我在 NSTextView 中观察到奇怪的行为。
假设有多行(通过按 Enter 键分隔),当我持续按 Tab 键时,整个段落会变成项目符号行。
我确实设置了 tabStops 并使标尺能够看到 tabStops,如中所述 Premature line wrapping in NSTextView when tabs are used
对于一个空的 NSTextView 它工作得很好,但是当我将它应用到现有文本时,即使正确设置了 tabStop,按下 Tab 时也会出现这种奇怪的行为:变成带项目符号的段落。
这是我的代码,用于检索 NSTextView 中的现有字符串并设置 tabStops
- (IBAction)formatTextView:(EditorTextView *)editorTextView tableWidth:(double) width
{
int cnt;
int numStops;
int tabInterval = 30;
NSTextTab *tabStop;
//attributes for attributed String of TextView
NSMutableDictionary* attrs = [[[editorTextView textStorage] attributesAtIndex:0 effectiveRange:NULL] mutableCopy];
NSParagraphStyle *paraStyle = [attrs objectForKey:NSParagraphStyleAttributeName];
NSMutableParagraphStyle *paraStyleM = [paraStyle mutableCopy];
// 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)];
[paraStyleM addTabStop:tabStop];
}
[attrs setObject:paraStyleM forKey:NSParagraphStyleAttributeName];
[[editorTextView textStorage] addAttributes:attrs range:NSMakeRange(0, [[[editorTextView textStorage] string] length])];
}
最佳答案
您现有的文本是来自 HTML 的吗?我猜其中发生了某种
- 事情。
他们最近(最近六年?)将 HTML 有序列表和无序列表支持黑入 NSTextView 中,以支持邮件中更丰富的消息,但它仍然相当丑陋。
关于objective-c - 按下 Tab 时 NSTextView 中的奇怪行为(即使设置了 tabStops),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21579569/