How to disable user interaction of NSTextView? - swift

In Cocoa Touch, you can disable user interaction of a text view like this:
let textView = UITextView()
textView.isUserInteractionEnabled = false
But in Cocoa, NSTextView has no such easy API to use.
So what's the proper way to disable NSTextView user interaction?

You can disable its' editable by:
textView.isEditable = false
And its' selectable by:
textView.isSelectable = false
However, when you disable both of them. This textView still will intercept Mouse-down event, which is different from isUserInteractionEnabled .
So, for now, to enable click-through, my solution is subclassing a NSTextView and override its' hitTest:
class TextView: NSTextView {
override func hitTest(_ point: NSPoint) -> NSView? {
return nil
}
}
An encapsulated way is here
class StaticTextView: NSTextView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
}
override init(frame frameRect: NSRect, textContainer container: NSTextContainer?) {
super.init(frame: frameRect, textContainer: container)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func hitTest(_ point: NSPoint) -> NSView? {
return nil
}
}

Related

Add Custom NSView into NSStackView

I have a Custom NSView. This view had a xib with all desing
import Cocoa
class CustomView: NSView {
#IBOutlet var contentView: NSView!
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
Bundle.main.loadNibNamed("CustomView", owner: self, topLevelObjects: nil)
addSubview(contentView)
contentView.frame = contentView.bounds
contentView.autoresizingMask = [.height, .width]
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here.
}
}
When I try to add this view into NSStackView doesn't work.
This is the code in my ViewController
override func viewDidLoad() {
super.viewDidLoad()
let view = CustomView()
self.stackView.addArrangedSubview(view)
}
Any idea.

Subclass of NSDatePicker mouseDown event changes datePicker instance

I have instantiated and initialized a subclass of NSDatePicker in AppDelegate -> applicationDidFinishLaunching and overrode mouseDown(with event: NSEvent)in the datePicker subclass. When I press the mouse button in the datePicker and break in its overridden mouseDown func the instance is not the one I instantiated in applicationDidFinishLaunching and so is not initialized.
I've tried creating the instance at different entry points thinking it might be a timing thing but I've gotten nowhere. I'm out of ideas and feeling a little feeble. Any Help?
The datePicker:
import Cocoa
class AlarmIVDatePicker: NSDatePicker {
var viewController: ViewController!
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
}
override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
return true
}
override func mouseDown(with event: NSEvent) {
let stop = 0
}
}
The ViewController:
class ViewController: NSViewController, NSWindowDelegate{
var alarmIVDatePicker: AlarmIVDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
alarmIVDatePicker = AlarmIVDatePicker()
alarmIVDatePicker.viewController = self
}
I expected I could access the values I had set but the instance is not the one I created and all the values are nil
Okay. Here's what you can do to track this down. Add this code to your AlarmIVDatePicker class:
override init(frame frameRect: NSRect) {
Swift.print("AlarmIVDatePicker being called here")
super.init(frame: frameRect)
}
convenience init() {
self.init(frame: .zero)
}
required init?(coder: NSCoder) {
Swift.print("hey, you DID drop this into a storyboard or XIB file!")
super.init(coder: coder)
}
If you set breakpoints inside these init methods, you'll catch when and where they are being called where you did not expect.

Live Interface Builder update with #IBDesignable for custom NSSlider control

I have problems in getting my custom NSSlider control updated live within Xcode's Interface Builder.
I have implemented #IBDesignable and prepareForInterfaceBuilder as shown in many other posts and tutorials. My little test just removes the knob from the slider control.
Here is the code I am using at the moment:
import Cocoa
#IBDesignable
class ColorSlider2: NSSlider {
override func setNeedsDisplay(_ invalidRect: NSRect) {
super.setNeedsDisplay(invalidRect)
}
override func awakeFromNib() {
super.awakeFromNib()
setupView()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setupView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
}
private func setupView() {
if ((self.cell?.isKind(of: ColorSlider2Cell.self)) == false) {
let cell = ColorSlider2Cell()
self.cell = cell
}
self.alphaValue = 0.5
self.floatValue = 0.4
}
}
class ColorSlider2Cell: NSSliderCell {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init() {
super.init()
}
override func drawKnob(_ knobRect: NSRect) {
return
}
}
The preview in Interface Builder is neither removing the knob nor updating the floatValue:
Do you have any idea why this is the case?

extension for UITextfield keyboard appearance

in my app, I will add different themes, but for now, I want to write an extension that can handle keyboard appearance from extension.
for the first step, I just want to have control of its appearance, so I set that it should be .dark but it's not working. could you tell me where is the problem in this simple extension?
I just want it to change the keyboard appearance automatically without doing anything else
extension UITextView {
var keyboardApperance: UIKeyboardAppearance? {
get {
return self.keyboardAppearance
}
set {
self.keyboardAppearance = .dark
}
}
}
Creating just an extension doesn't change your UITextView properties.
You can create your own custom UITextView and use it instead of UITextView.
Don't forget to set your class if you are using storyboard or xib.
#IBDesignable
public class CustomDarkTextView: UITextView {
public override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
setup()
}
public convenience init(frame: CGRect) {
self.init(frame: frame, textContainer: nil)
setup()
}
public convenience init() {
self.init(frame: CGRect.zero, textContainer: nil)
setup()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
self.keyboardAppearance = UIKeyboardAppearance.Dark
}
}
trying using functions like this
extension UITextView {
func setKeyboardToDark() {
self.keyboardAppearance = .dark
}
func setKeyboardToLight() {
self.keyboardAppearance = .light
}
}
extension UITextView {
func setDarkKeyboard() {
self.keyboardAppearance = UIKeyboardAppearance.dark
}
func setLightKeyboard() {
self.keyboardAppearance = UIKeyboardAppearance.light
}
}

Track Touch Events without app being focused - Swift

I made a simple project in Xcode to track the position of a touch event on a trackpad. I created a Custom View instance on my MainWindowController.xib for the class TrackpadController:
class TrackpadController: NSView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect);
self.acceptsTouchEvents = true
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func touchesBeganWithEvent(event: NSEvent) {
let touches = event.touchesMatchingPhase(.Began, inView: nil)
for touch in touches {
let touchPosition = touch.normalizedPosition
Swift.print(touchPosition)
}
}
}
The coordinates are printed in the console, but only if the cursor is over the view. I would like to be able to track the position on the trackpad when the app isn't focused. Any idea in how to get this would be great. Thanks.