Here is the solution in Swift. It's already late, and I'm tired, so this is probably not optimal, but it works.
First, here is the function to find the view within the hierarchy, with the ability to skip a specific view. (Which is useful if we search through window.contentView.superview.subviews and we want to ignore your own views in the contentView )
func findViewInSubview(subviews: [NSView], #ignoreView: NSView, test: (NSView) -> Bool) -> NSView? { for v in subviews { if test(v) { return v } else if v != ignoreView { if let found = findViewInSubview(v.subviews as [NSView], ignoreView: ignoreView, test) { return found } } } return nil }
And here is how you use it, for example, from a subclass of NSViewController . Note that you need to do this when the window has become visible, so you cannot do this in viewDidLoad .
override func viewDidAppear() { if let windowContentView = view.window?.contentView as? NSView { if let windowContentSuperView = windowContentView.superview { let titleView = findViewInSubview(windowContentSuperView.subviews as [NSView], ignoreView: windowContentView) { (view) -> Bool in // We find the title by looking for an NSTextField. You may // want to make this test more strict and for example also // check for the title string value to be sure. return view is NSTextField } if let titleView = titleView as? NSTextField { titleView.attributedStringValue = NSAttributedString(string: "Hello", attributes: [NSForegroundColorAttributeName: NSColor.redColor()]) } } } }
Remember that you play with fire. Internal ones like this are not indicated for any reason.
Stefan arentz
source share