【问题标题】:Displaying text in a NSScrollView (Swift)在 NSScrollView (Swift) 中显示文本
【发布时间】:2014-08-07 09:33:08
【问题描述】:

刚开始学习 Swift 并创建了一个小的 macOS 应用程序,我想在其中使用 NSScrollView 来显示属性字符串。我试过了:

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

但这似乎不起作用。感觉很困惑,因为用 NSTextView 做同样的事情反而像魅力一样。

我在这里错过了什么?

【问题讨论】:

    标签: xcode swift nsscrollview nsmutableattributedstring


    【解决方案1】:

    在下面的示例中,我在界面构建器中添加了滚动视图作为我的起点。

    如果您的scrollView/textView 为空/空白,或者如果您需要在scrollView/textView 中已有的内容前面附加文本,则以下方法有效。如果框中已有文本,则将新文本插入现有文本的前面。

    documentView 是一个 NSTextView

    Swift 4.0

    @IBOutlet weak var imageDestinationDirectory: NSScrollView!
    
    ...
    
    let destinationText = "Text to display"
    imageDestinationDirectory.documentView!.insertText(destinationText)
    

    【讨论】:

      【解决方案2】:

      Xcode 的早期 beta 版本(以 Swift 为特色)似乎在此类插座方面存在一些严重问题(如您在此处看到的:http://swiftwtf.tumblr.com/post/88387419568/nstextview)。 但是,因为 Xcode 6 Beta 6 它可以工作。

      textFieldOutlet.textStorage.mutableString.setString("Hello w0rld!")
      

      要处理字符串,为textField 创建一个出口而不是scrollView 本身也是一种更好的做法。

      【讨论】:

        【解决方案3】:

        Apple 有一篇关于以编程方式在滚动视图中设置文本视图的文章。 Text System User Interface Layer Programming Guide: Putting an NSTextView Object in an 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】:

          以下是如何以编程方式使用自动布局:

          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)
                  ])
              }
          }
          

          【讨论】:

            【解决方案5】:
            @IBOutlet weak var scrollView: NSScrollView!
            

            NSScrollViewdocumentViewNSTextView,因此在 Swift 中您可以使用以下内容:

            let textView : NSTextView? = scrollView?.documentView as? NSTextView
            textView?.string = text
            

            【讨论】:

              【解决方案6】:

              ixany 的回答效果很好,但作为初学者,我需要更多时间来弄清楚,应该为 NSTextView 而不是 NSTextScroll 完成该出口。 NSTextView 隐藏在 NSTextScroll 内部。 All Results Field is NSTextScroll in my case and Text View Results is NSTextView

              【讨论】:

                【解决方案7】:

                对于 NSScrollView:

                @IBOutlet weak var textScroll: NSScrollView!
                

                看看这个:

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

                这非常符合@mrtn.lxo 解决方案的精神,仅针对 NSScrollView 完成并且正在使用 NSAttributedString。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-11-18
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-11-07
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多