Display text using NSScrollView

I just started learning Swift and created a small OSX application that I want to use NSScrollViewfor display NSMutableAttributedString. I tried:

@IBOutlet var ScrollViewOutlet : NSScrollView
var attributedString = NSMutableAttributedString(string: myText)
ScrollViewOutlet.insertText(attributedString)

... but unfortunately it does not work. it's a bit confusing because instead NSTextViewi have no problem. Where is my mistake?

+4
source share
6 answers

it seems that previous beta versions of xcode had some problems with these types (as you can see here swiftwtf.tumblr.com/post/88387419568/nstextview), however, since xcode 6 beta 6 works.

, NSScrollView NSTextView, , textStorage

textFieldOutlet.textStorage.mutableString.setString("your string here")

... .

+5

Apple , . : NSTextView NSScrollView. , :

NSScrollView *scrollview = [[NSScrollView alloc] initWithFrame:[[theWindow contentView] frame]];
NSSize contentSize = [scrollview contentSize];

[scrollview setBorderType:NSNoBorder];
[scrollview setHasVerticalScroller:YES];
[scrollview setHasHorizontalScroller:NO];
[scrollview setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

theTextView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, contentSize.width, contentSize.height)];
[theTextView setMinSize:NSMakeSize(0.0, contentSize.height)];
[theTextView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[theTextView setVerticallyResizable:YES];
[theTextView setHorizontallyResizable:NO];
[theTextView setAutoresizingMask:NSViewWidthSizable];

[[theTextView textContainer] setContainerSize:NSMakeSize(contentSize.width, FLT_MAX)];
[[theTextView textContainer] setWidthTracksTextView:YES];
[scrollview setDocumentView:theTextView];
[theWindow setContentView:scrollview];
[theWindow makeKeyAndOrderFront:nil];
[theWindow makeFirstResponder:theTextView];

, textStorage:

theTextView.textStorage.attributedString = attributedString;
+4

scrollView .

, scrollView/textView / , scrollView/textView. , .

DocumentView - NSTextView

Swift 4.0

@IBOutlet weak var imageDestinationDirectory: NSScrollView!

...

let destinationText = "Text to display"
imageDestinationDirectory.documentView!.insertText(destinationText)
+2

NSScrollView:

@IBOutlet weak var textScroll: NSScrollView!

:

let text = textScroll.documentView!.textStorage!!
let attr = NSAttributedString(string: "Hello World")

text.appendAttributedString(attr)

@mrtn.lxo, NSScrollView NSAttributedString.

+1

ixany , , , , , , NSTextView, NSTextScroll. NSTextView NSTextScroll. - NSTextScroll , - NSTextView

+1
source

Here's how to do this programmatically, with an automatic layout:

class ViewController: NSViewController {
    let scrollView = NSScrollView()
    let textView = NSTextView()

    override func viewDidLoad() {
        super.viewDidLoad()

        textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
        textView.autoresizingMask = .width
        textView.isVerticallyResizable = true
        textView.textContainer?.widthTracksTextView = true

        view.addSubview(scrollView)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.documentView = textView

        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.topAnchor),
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
    }
}
+1
source

All Articles