Как автоматически прокрутить одну строку NSTextView с горизонтально центрированным текстом? - PullRequest
0 голосов
/ 04 октября 2018

Я программно создаю 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];

Я пробовал каждую комбинациюнастроек setWidthTracksTextView, setHorizontallyResizable и setContainerSize:ZZZ (ZZZ был либо FLT_MAX, либо фактической шириной кадра NSTextView) - но безрезультатно

Относительно комментария, помеченного * СМ.ПРИМЕЧАНИЕ НИЖЕ *, если я не изменю значения 'horizontallyResizable' и 'widthTracksTextView' на основе выравнивания, ширина контейнера ([[view textContainer] containerSize]) в конечном итоге станет равной 0, хотя я указал FLT_MAX в качестве его ширины,Это не тот случай, когда alignemnt равен NSNaturalTextAlignment.

Заранее спасибо за помощь.

...