我正在尝试以编程方式在NSTextView
中设置NSScrollView
并遇到问题。
具体来说,我无法滚动文字视图;滚动视图似乎认为“这就是全部”。当您尝试向下滚动以查看其余内容(未发布)时,结果如下:
一旦释放,滚动视图就会反弹,因此它不是图形故障;它只是在框架高度剪辑文本视图。
我尝试使用the Apple 'Text In ScrollView' docs进行试验,并尝试使用setVerticallyResizable:
等。
代码:
- (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)]];
}
稍后将帧调整为正确的值,
[_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}]];
就是这样。提示欢迎!
答案 0 :(得分:2)
您已设置垂直约束,使文本视图与其超级视图的高度匹配(滚动视图的剪辑视图)。我认为那是错的。如果你完全删除了它,你就会在垂直方向上完全模糊,文本视图可以在任何地方结束。我认为你可以将其顶部边缘固定在superview的顶部 - 即@"V:|[_valueField]"
。如果文本视图提供了内在高度,那将会有效,但我不确定它是否有效。为文本视图的高度设置约束,使其与文本布局管理器生成的内容相匹配是非常重要的。
鉴于您在布局中并不需要任何特殊内容,您可以使用旧式自动调整遮罩而不是自动布局。
答案 1 :(得分: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)
}
}