Disable editing in NSTextView - macos

I am trying to disable editing in NSTextView but there doesn't seem to be any option there.All the other types have enabled property which when set to false are non editable but is there any for NSTextView?

Please do not forget that a class inherits the methods of its superClass. The superClass of NSTextView is NSText. And there you find the method
(void)setEditable:(BOOL)flag
with the comment:
Controls whether the receiver allows the user to edit its text.

For SWIFT3, you need to set isEditable to false.
var isEditable: Bool
A Boolean value that controls whether the text views sharing the receiver’s layout manager allow the user to edit text.
https://developer.apple.com/documentation/appkit/nstextview#overview

Related

NSTextField set disabled text color

I have a NSTextField which is enabled or disabled. I can set the textColor but this only has an effect on the color of the text while the text field is enabled.
Any ideas how to set the text color for the disabled state?
I already tried to subclass NSTextField and override the enable methods as suggested in Disable NSTextField without changing color of multi-colored text ... but that doesn't work for me. I guess because of the latest SDK supporting dark mode.
Simple approach is to override resignFirstResponder:
override func resignFirstResponder() -> Bool {
//change text color here...
return super.resignFirstResponder()
}
You can simply add your colour change code for whichever controls you like here...
As stated by Apple:
The default implementation returns true, resigning first responder status. You can override this method in your custom responders to update your object's state or perform other actions, such as removing the highlight from a selection. You can also return false, refusing to relinquish first responder status. If you override this method, you must call super (the superclass implementation) at some point in your code.

Set default values for inherited IB-properties

I wrote a UILabel-subclass called Badge that is supposed to display notifications similar to Apple's icon-badges. I made Badge customizable in Interface Builder by prefixing it with #IBDesignable and its properties with #IBInspectable. The setup works both from code and IB, where it is rendering properly. The only thing I'm struggling with is setting up new default-values for the inherited #IBDesignable-properties such as text and backgroundColor. I have tried to set these values in the init-methods and also prepareForInterfaceBuilder(), but the values in the Attributes Inspector in the UILabel and UIView-section remain unchanged.
Here my question:
Is it possible to change/override the UILabel's default text found in the Attributes Inspector so that a custom text instead of "Label" is rendered in IB for my subclass by default?

How do I disable or remove the "replace" checkbox of NSTextFinder?

My app displays a logfile in an NSTextView. I want the user to be able to search the output using the find bar, but not be able to replace any text. The NSTextView is set to be not editable, but when I press cmd-F, the find bar shows a "replace" checkbox:
I can prevent the user from being able to actually replace any text by implementing the NSTextViewDelegate protocol methods textView:shouldChangeTextInRange:replacementString: and
textView:shouldChangeTextInRanges:replacementStrings: but this does not remove or disable the checkbox. How can I do this?
NSTextFinder won’t present the Replace box if its findBarContainer’s editable property is NO (false).
You can set the property in code (the API is in the rarely-used superclass, NSText) or uncheck the box in Interface Builder.

Cocoa: How to bind a boolean property to NSCellStateValue?

I would like to bind the boolean enabled property of an NSTextField to the state of an NSButton. I already tried adding a custom NSValueTransformer that transforms the state of the NSButton into NSNumber. However, in that scenario the text fields are disabled all the time for some reason. My second approach: To bad fails also since NSValueTransformer does not offer return primitives types such as BOOL.
Example:
The screenshot shows an example in which the text fields are disabled because the checkbox has the state NSOnState. I also would like to bind the labels to this state.
Further, it would be convenient, if I could set a "disabled text" in Interface Builder. In the above example I set the text in the associated class.
Edit:
I set self.anonymousLoginCheckbox.state as the Model Key Path for the enabled property of the account text field. Similar for the password text field. However, it does not work.
Update:
I created an example project available on GitHub showing the implementation kindly described by Nicolas Bachschmidt.
NSButton isn't KVO compliant for the key state. Cocoa Bindings require the observed object to emit notifications when the observed property changes. As NSButton's state is just a wrapper for its cell's state, -[NSButton setState:] method (and the automatic KVO notifications) isn't invoked when the user click the button (but -[NSCell setState:] is). If you set the model key path to self.anonymousLoginCheckbox.cell.state, it will work.

How do I bind an NSMenuItem to an NSArrayController

How do I bind the enabled state of an NSMenuItem to an NSArrayController's selection? I've tried binding the item's enabled state to the controller's selectedObjects or selectedIndexes and in neither case is the menuitem ever enabled when there are selections. In IB, I unchecked the "enable" checkbox. I simply want the NSMenuItem to be enabled when there are selections in the table. My table allows for multiple selection and I also use a button which is bound to selectedObjects.#count and the button enables/disables as expected, so I thought using the same keypath would work for the menuitem as well, but nope. This can't be difficult as I can't find an answer via google, so I figure it must be simple.
Thanks
The enabled binding has to get a BOOL value, and unfortunately won't just treat any old object as a boolean True. Fortunately, however, NSValueTransformer makes it easy to do so. There's a couple of constants named in the NSValueTransformer Class Reference which you can use in the bindings pane in IB.
In your case, you can bind the Model Key Path to "selectedObjects" and enter "NSIsNotNil" in the Value Transformer field. The transformer gives the binding the BOOL value it needs.

Resources