objective-c - 按下 Tab 时 NSTextView 中的奇怪行为(即使设置了 tabStops)

标签 objective-c macos cocoa

我在 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/

相关文章:

objective-c - 先在/Application文件夹中搜索应用程序

ios - iOS : EAWiFiUnconfiguredAccessoryBrowser will detect unconfigured accessories only once 无线配件配置

iphone - 从 NSMutableArray 中的元素中删除字符?

linux - 在 Mac OSX 上交叉编译到 x86_64-unknown-linux-gnu 失败

excel - Mac : How to convert Excel (and other) file types to PDF programmatically

xcode - 从苹果的源代码构建 DYLD

macos - 在 NSAttributedString 的一部分周围绘制边框

ios - RestKit 对象与外键和服务的映射

cocoa - 如何从 OpenGL 纹理创建 CVOpenGLTextureRef

macos - 如何使用 swift 为 osx webview 设置自定义用户代理