Programmatically configure NSTextView inside NSScrollView

I am trying to configure NSTextViewin NSScrollViewsoftware and am having problems.

In particular, I cannot scroll the text view; in the scroll view it seems that "that's all there is." When you try to scroll down to see the rest of the content (without release), this is the result:

Clipped

As soon as you release, the scroll view bounces back, so this is not a graphic glitch; it just clamps the text view at the height of the frame.

I tried experimenting with Apple Text In ScrollView docs and I experimented with setVerticallyResizable:etc.

The code:

- (void)configureValueTextView
{
    NSScrollView *vfContainer = [[NSScrollView alloc] initWithFrame:(NSRect){0,0,50,50}];
    vfContainer.hasVerticalScroller = YES;
    vfContainer.hasHorizontalScroller = NO;
    vfContainer.translatesAutoresizingMaskIntoConstraints = NO;
    _valueFieldContainer = vfContainer;
    vfContainer.borderType = NSLineBorder;

    NSTextView *valueField = _valueField = [[NSTextView alloc] initWithFrame:(NSRect){0,0,vfContainer.contentSize}];
    valueField.delegate = self;
    valueField.minSize = (NSSize){0,0};
    valueField.maxSize = (NSSize){FLT_MAX, FLT_MAX};
    [valueField setVerticallyResizable:YES];
    [valueField setHorizontallyResizable:YES];

    valueField.translatesAutoresizingMaskIntoConstraints = NO;
    valueField.string = _value ? _value : @"";
    valueField.editable = YES;
    valueField.textContainer.containerSize = (NSSize) { vfContainer.contentSize.width, FLT_MAX };
    valueField.textContainer.heightTracksTextView = NO;

    vfContainer.documentView = valueField;
    [vfContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[_valueField]-(0)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_valueField)]];
    [vfContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[_valueField]-(0)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_valueField)]];
}

Frames are later adjusted to their right values,

[_valueFieldContainer setFrame:(NSRect){kKEFormFieldViewKeyWidth + 10, 0, valueFieldWidth, h}];
[_valueField setFrame:(NSRect){0, 0, valueFieldWidth, h}];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[_valueFieldContainer]-(5)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_valueFieldContainer)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[_keyField]-(10)-[_valueFieldContainer]-(0)-|" options:0 metrics:nil views:@{@"_keyField": _keyField, @"_valueFieldContainer": _valueFieldContainer}]];

What is it. Tips are welcome!

+2
2

, ( ). , . , , . , , - .. @"V:|[_valueField]". , , , . , , , .

, - , .

+2

Swift 4.

class ViewController: NSViewController {

   fileprivate lazy var textStorage = NSTextStorage()
   fileprivate lazy var layoutManager = NSLayoutManager()
   fileprivate lazy var textContainer = NSTextContainer()
   fileprivate lazy var textView: NSTextView = NSTextView(frame: CGRect(), textContainer: textContainer)
   fileprivate lazy var scrollview = NSScrollView()

   override func viewDidLoad() {
      super.viewDidLoad()
      view.addSubview(scrollview)
      // Set `true` to enable horizontal scrolling.
      setupUI(isHorizontalScrollingEnabled: false) 
      setupLayout()
      setupTextStack()
   }
}

extension ViewController {

   fileprivate func setupTextStack() {
      textStorage.addLayoutManager(layoutManager)
      layoutManager.addTextContainer(textContainer)
   }

   fileprivate func setupUI(isHorizontalScrollingEnabled: Bool) {

      let contentSize = scrollview.contentSize

      if isHorizontalScrollingEnabled {
         textContainer.containerSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
         textContainer.widthTracksTextView = false
      } else {
         textContainer.containerSize = CGSize(width: contentSize.width, height: CGFloat.greatestFiniteMagnitude)
         textContainer.widthTracksTextView = true
      }

      textView.minSize = CGSize(width: 0, height: 0)
      textView.maxSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
      textView.isVerticallyResizable = true
      textView.isHorizontallyResizable = isHorizontalScrollingEnabled
      textView.frame = CGRect(width: contentSize.width, height: contentSize.height)
      if isHorizontalScrollingEnabled {
         textView.autoresizingMask = [.width, .height]
      } else {
         textView.autoresizingMask = [.width]
      }

      scrollview.borderType = .noBorder
      scrollview.hasVerticalScroller = true
      scrollview.hasHorizontalScroller = isHorizontalScrollingEnabled
      scrollview.documentView = textView
   }

   fileprivate func setupLayout() {
      scrollview.translatesAutoresizingMaskIntoConstraints = false

      var customConstraints = [NSLayoutConstraint]()
      // `LayoutConstraint` - custom type with convenience methods. 
      customConstraints += LayoutConstraint.pinInSuperview(scrollview)
      view.addConstraints(customConstraints)
   }
}
0

Source: https://habr.com/ru/post/1548208/


All Articles