0

OS X でカスタム描画を行う方法を独学しようとしています。NSView 内に NSTextView をネストしようとしています。

上に行き、下に行きたい

別のカスタムビューに埋め込まれていないかのように動作させるために欠けているステップを理解できないようですNSTextView(つまり、テキストは、に提供されたフレームの左上から改行を開始する必要がありますNSTextView、左から-右と上から下)。

import Cocoa
import AppKit
import XCPlayground

class MyView : NSView {
    let lipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

    override init(frame: NSRect) {
        super.init(frame:frame)
        self.wantsLayer = true
    }

    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)

        let grey = NSColor.grayColor()
        self.layer?.backgroundColor = grey.CGColor

        let boundaryRect = NSInsetRect(dirtyRect, 10, 10)
        let textRect = NSInsetRect(boundaryRect, 10, 10)

        let path = NSBezierPath(roundedRect: boundaryRect, xRadius: 5, yRadius: 5)
        path.stroke()


        let text = NSTextView(frame: textRect)

        text.backgroundColor = grey
        text.insertText(lipsum)

        text.drawRect(textRect)
    }
}

var frame = NSRect(x: 0, y: 0, width: 400, height: 400)

let myView = MyView(frame: frame)

let textView = NSTextView(frame:frame)
textView.insertText(myView.lipsum)

XCPShowView("my view", myView)
XCPShowView("text view", textView)
4

1 に答える 1

1

text親ビュー内にビューを実際に埋め込んでいません。でそれを行うことができますaddSubview()

    let text = NSTextView(frame: textRect)

    text.backgroundColor = grey
    text.insertText(lipsum)

    self.addSubview(text) // <---------
}
于 2014-08-31T15:44:52.880 に答える