使用制表符时,NSTextView中的过早行换行 [英] Premature line wrapping in NSTextView when tabs are used

查看:378
本文介绍了使用制表符时,NSTextView中的过早行换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,NSTextView linewrapping之后的第51列,如果我输入一行选项卡。这只会发生在标签页,而不是任何其他字符,它正确包裹在文本视图的边缘,而不是在第51个字符之后。



这很容易重复。在XCode中使用单个窗口和一个NSTextView创建一个空白项目。唯一的非默认设置是我已删除约束,并使用旧样式自动调整自动调整textview以便它填充窗口。我没有写任何代码。现在运行应用程序,打开窗口,使其比51个字符宽得多,按住tab键,它会提前换行。



提前感谢。这里的问题是,NSTextView有一个默认的NSMutableParagraphStyle对象,它有一个属性列表,例如换行,制表符,边距,属性,你可以看到这个,通过去格式化菜单,文本子视图,并选择显示标尺菜单。



显示标尺后,您将看到所有标签页,这将解释为什么您的标签页包装一次你到达最后一个标签页。



所以你需要的解决方案是创建一个标签数组,你想为你的段落样式对象,然后设置为样式对于NSTextView。



这里是一个创建选项卡的方法。在这个例子中,它将创建5个左对齐的选项卡,每隔1.5英寸:

   - (NSMutableAttributedString *)textViewTabFormatter: *)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 * attributingString = [[NSMutableAttributedString alloc] initWithString:aString];
[attributString addAttribute:NSParagraphStyleAttributeName value:aMutableParagraphStyle range:NSMakeRange(0,[aString length])];

return attributString;
}

然后,在将任何文本添加到NSTextView之前,

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

如果您想要更深入的教程,可以在这里找到其他信息:



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


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.

解决方案

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

这篇关于使用制表符时,NSTextView中的过早行换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆