2014-08-31 1 views
0

OS X에서 사용자 정의 도면을 수행하는 방법을 스스로 가르쳐려고합니다. NSView 내에 NSTextView를 중첩 시키려고합니다.Appkit/Quartz/CG 사용자 정의보기 내에서 NSTextView를 올바르게 중첩

Got top, want bottom

나는 그것이 다른 사용자 지정보기에 포함되지 않은 것처럼 NSTextView 행동을 얻을 놓치고 단계를 알아낼 수없는 것 (즉, 텍스트는 왼쪽 상단에서 redering 시작해야 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) 

답변

1

실제로는 부모보기 내에 text보기를 포함하지 않았습니다. addSubview()을 사용하면됩니다.

let text = NSTextView(frame: textRect) 

    text.backgroundColor = grey 
    text.insertText(lipsum) 

    self.addSubview(text) // <--------- 
} 
+0

그래. 얼마나 간단합니다. Learn-by-doing은 단점이 있습니다;). 이제 나는 아이에게 무승부를 요구할 필요조차 없습니다. –

+0

Learn-by-doing, 가장 기억에 남는 학습 방법! – chuckus

관련 문제