Set default values for inherited IB-properties - xcode

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?

Related

How to remove translucency that appairs on some parts of a NSTableView?

I'm working on a project for macOS with Storyboards where I have a custom view containing a NSTableView and some other controls.
The customView background is colored in viewWillAppear():
backgroundCustomView.layer?.backgroundColor = NSColor.white.cgColor
The problem is that the items of the NSTableView appears with some translucent parts inside them.
I would like to prefer that the NSTableView appears without any translucency (this is the reason because I put a custom view under the NSTableView).
I tried to change in IB about all what I think could be related options without any luck.
PS
The discontinued translucency between the items is due by the cell spacing option.
You need to specified that you view needs a layer as its backing store
backgroundCustomView.wantsLayer = true
backgroundCustomView.layer?.backgroundColor = NSColor.white.cgColor
More info at wantsLayer property documentation page

Change object color in Xcode

I'm new to Xcode (Mac development, not iOS), and for some reason I can't figure out how to even change any object's color (text, background or really anything). Every site seems to say to click on the object, go to the attributes inspector and all of those options are under 'view'. However, in my Xcode (5.1.1) all it shows under 'view' is tag, focus ring, drawing, and auto-resizing. Am I missing something obvious?
On OS X, NSView does not have an intrinsic backgroundColor property. Thus, you cannot set the color of a view from Interface Builder. You have to create an NSView subclass and override -drawRect: or -updateLayer to make it the color you want. Even then, that color will not show up in Interface Builder. (This changes in Xcode 6, which is still in beta as of this writing.)
Is this somewhat annoying? Yeah. But that's the way it is.
As for changing the text of an object, you should be able to do it from the Attributes inspector, but only if it's something that already has text (i.e. a text field, text view, or button). An arbitrary custom view does not have text, so you can't set it in Interface Builder.
You should easily be able to set the background color of whatever UI element you want programmatically.
[viewObject setBackgroundColor:[UIColor greenColor]];
As for editing a xib for OSX I'm not sure. I have only done iOS development.

Specify editing NSTextField inside NSPopover on appearance

I'm working on an app that presents an NSPopover containing a number of NSTextFields. While I can tab between these fields as I expect, the popover is selecting a particular text field to be in the editing state when it appears, and it's not the field I want to edit; I'd like to be able to define which text field is editing on popover appearance programmatically (or in Interface Builder). How can I do this?
I've set up the appropriate key view loop by connecting IB outlets for all the various text fields involved, and I've hooked up the popover's nextResponder property to the text field I want to edit first, but that doesn't seem to have an effect - the popover will still select its preferred text field instead of mine. The Window Programming Guide suggests that I set the initialFirstResponder outlet of the window to the view I want selected, but an NSPopover is not an NSWindow and has no initialFirstResponder property (unless I'm missing something obvious).
Is there any way to specify which NSTextField I want to be editing when an NSPopover appears?
I think you said you tried using -makeFirstResponder: and passing the text field. This will set the window's firstResponder, but that's not the same as initialFirstResponder and the window must have initialFirstResponder set to something other than nil in order to respect the key view loop. (Source) A slight tweak to what you tried worked for me:
- (void)popoverWillShow:(NSNotification *)notification
{
// Set the window's initialFirstResponder so that the key view loop isn't auto-recalculated.
[[myField window] setInitialFirstResponder:myField];
}
I think you can make this work by setting all the text field's that you don't want to have focus to "selectable" instead of "Editable" in IB, this should leave the one text field you want to start with as the first responder. Then, in your popoverDidShow: method, set them all back to editable, and you should be able to tab between them as usual.

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.

Cocoa editable text with no input box

I am trying to make an editable text object in cocoa that contains no input box (just the text). I have tried doing this using NSTextField but setDrawsBackground: NO and setBordered: no have not helped. Is there another method for NSTextField that will do this or do I need to use another class? I looked at NSText but this class seems to just be the same as NSTextField with less functionality.
You're headed in the right direction. Are you missing - setBezeled:NO?
It's fairly simple to get what you're looking for through Interface Builder. The only changes required (from its default instance) are to set the border to the dashed (invisible) option and uncheck the Draws Background.

Resources