如何使用水平居中文本自动滚动单行NSTextView?

时间:2018-10-04 07:44:56

标签: macos cocoa scroll nstextview

我正在以编程方式创建NSTextView。当对齐方式为NSNaturalTextAlignment时,如果文本宽于包围的矩形,则文本将自动滚动。但是,如果我使用NSCenterTextAlignment,则自动滚动行为不起作用。

这是我用来创建NSTextView的代码:

// create the scroll view
NSScrollView* scrollview = [[NSScrollView alloc] initWithFrame:inSuperviewFrame];

[scrollview setBorderType:NSNoBorder];
[scrollview setHasHorizontalScroller:NO];
[scrollview setHasVerticalScroller:NO];

// create the text view
NSTextView* textView = [[NSTextView alloc] initWithFrame:inSuperviewFrame];

BOOL horizontallyResizable = YES;
BOOL widthTracksTextView = NO;

// the following is required for centered text
// to be drawn at all within the container
// *** SEE NOTE BELOW ***
if (inHorzAlignment == NSCenterTextAlignment) {
    horizontallyResizable = NO;
    widthTracksTextView = YES;
}

[textView setAlignment:inHorzAlignment];

[textView setMinSize:NSMakeSize(0, 0)];
[textView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];

// will resize to accomodate text horizontally but not vertically
[textView setHorizontallyResizable:horizontallyResizable];
[textView setVerticallyResizable:NO];
// set unlimited container width and fixed height
[[textView textContainer] setContainerSize:NSMakeSize(FLT_MAX, inSuperviewFrame.size.height)];
// specify if text container tracks the text view's width or not
[[textView textContainer] setWidthTracksTextView:widthTracksTextView];
// the text container tracks the view's height
[[textView textContainer] setHeightTracksTextView:YES];

[textView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];


[scrollview setDocumentView:textView];

我尝试了setWidthTracksTextViewsetHorizontallyResizablesetContainerSize:ZZZ的设置的每种组合(ZZZ为FLT_MAX或NSTextView框架的实际宽度)-但无济于事

关于标记为 *的注释,请参阅* ,如果我不基于对齐方式更改容器的宽度,则不更改'horizo​​ntallyResizable'和'widthTracksTextView'的值,容器的宽度({{1} })即使我已将FLT_MAX指定为其宽度,最终还是为0。 alignemnt为[[view textContainer] containerSize]时不是这种情况。

预先感谢您的帮助。

0 个答案:

没有答案
相关问题