use UIMenuController with text - objective-c

So i would like to use the UIMenuController to display text. i would like to add in help buttons (press a little button and something will pop up describing what something does) that would display either a UIMenuController or something that looks similar to it. it would just display a few lines of text and dismiss when pressed.
it kinda looks like UIMenuController is only meant for button selection. i could just use one button and set the title to the message i wish to display, but am doubtful on how well that would work. are there any other options available?

A UIMenuController takes it content from its menuItems property and the documentation says :
The default value is nil (no custom menu items). Each menu item is an
instance of the UIMenuItem class. You may create your own menu items,
each with its own title and action selector, and add them to the
editing menu through this property. Custom items appear in the menu
after any system menu items.
So you are obliged to use UIMenuItem objects as content for your UIMenuController. UIMenuItem only inherits from NSObject, and stores only a title and an action, both required. This make you unable to use any other kind of data as UIMenuItem.
As it is not an UIView, you can give it a UIButton or a UIImageView.
You could think to override UIMenuController but again it is a direct child of NSObject, so it doesn't have any behavior for customization.
The only solution left to you, is to rewrite your own UIMenuController, deriving it from UIToolbar for example. This would give you more or less the same look, you just have to customize the arrow and the round corner.

Related

NSButton draw alternate image on click

I know this is probably super easy, but I'm having a hard time wading through header files and could use some guidance.
I have an NSPopUpButton defined in a nib with a menu attached to it. I have defined it in the nib to be "Pull Down" type with no arrow (the images given to me by our designer include a special arrow he wants to use). It is a borderless button. The title properly is empty, though I have set the image and alternateImage.
What I'm looking for is a configuration, either in the nib, file's owner implementation, or by subclass NSPopUpButton, that will allow me to display the button's alternate image (or any other image) while the button's menu is being displayed. Instead, it looks like the system is dimming the button's image.
I've tried numerous nib configurations, various properties of NSButton and NSButtonCell, overriding mouseDown and mouseUp. Nothing I do works.
If playing with the "Type" property doesn't help, you may well have to subclass the drawing of NSButtonCell to get what you want.
- (void)drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView in NSButtonCell seems promising.
I think it's custom NSButtonCell class time; I don't know of any way to override the theme for this type of button.
Your only alternative would be to use a regular NSButton with the appropriate image and show the menu at the correct position manually, which would probably be a lot easier to implement.

Xcode-style font picker

I'd like to add a font picker in my app. Xcode's Interface Builder has a great implementation which is used in Xcode's own preferences' window too.
It's the one with a little 'T' button (seemingly) inside the font name text field itself.
Is this a standard cocoa implementation?
You'll need to roll your own. See Subclassing NSControl. You'll subclass NSTextField and NSTextFieldCell. Read the entire guide, actually. Once you've got a good understanding, then you can override the drawing/geometry routines to return a rect whose width is slightly smaller (small enough to leave room for the font "button"). Then draw your font button. You'll respond to its mouse events on the font button the same as you would for any NSView.
There's no standard control for the preview text box with the button, but there is the standard NSFontPanel class that you can use to let users select the font once the 'T' button has been clicked.
Best idea is probably to override NSTextFieldCell. I've been able to get the desired appearance by overriding -drawInteriorWithFrame:inView:, and passing a slightly less wide frame when calling through to super, then drawing the button myself. You'd also have to implement the mouse tracking of the button yourself, but you could probably just create your own NSButtonCell instance and call through to it for a few methods with the sub-rect for the button.
For convenience, you may also want to create a NSTextField subclass that uses this cell instead of a straight NSTextFieldCell, but if you're loading stuff from a XIB, you can just change the class of the cell in the XIB and leave it in a regular text field view.

want to remove a specific collectionview item with the remove button on that view

I have a collection view item and its prototype view. Within that prototype view I have a little x button. I want that button to remove the exact collection view item that it is on top of.
I can remove a selected item if I click on the space around the x button but if I go straight to clicking the button before clicking the item it will only erase the last selected item.
ideas?
Alright, this should be fun!
Step one: Change the method signature of removeQuartzPlayer to be:
-(void)removeQuartzPlayer:(id)aPlayer;
Where aPlayer will be the player you want to remove. Change the implementation to look something like this:
[quartzPlayerArrayController removeObject:aPlayer];
Where quartzPlayerArrayController would be replaced with the name of the outlet to the NSArrayController that your collection view is connected to. I'll trust that you can figure out how to create an IBOutlet to it, if you don't already have one.
Step one-point-five: remove the connection between the button and the removeQuartzPlayer method in IB.
Step two: Switch to the Bindings pane of the IB Inspector. Select the "Target" binding and make it look like this:
Where the "Controller" popup would be set to whatever object points to your controller.
Step three: Select the "Argument" binding and make it look like this:
Where the "Collection View Item" popup... you get the idea.
Step four: Enjoy your new button, brought to you by the (dark) magic of Cocoa Bindings!
Disclaimer: I haven't actually tested this, but since I have a button that does something similar, it should work. Comment away if I've screwed something up.
Billy

UILabel inside a UIToolbar using IB is invisible on run, how fix?

I want to show a total inside a toolbar. I put in IB the UILabel on top of the toolbar .
However, when I run the app, the UILabel is totally invisible (but can set values on code fine).
The most bizarre thing is that in other form, all work fine. I don't see why in one form work but not in another...
Any idea in how fix this? Or why is this behaviour happening?
Don't use a UILabel.
Use a UIBarButtonItem. Then set it to style: plain. It looks like a label, but it actually borderless button. This is the general practice of displaying text on a bar.
You can also create UIBarButtonItem with a custom view in code. You are simple "wrapping" the UILabel in a UIBarButtonItem allowing you to add anything you want to a tool bar.
To add in response to comment:
Either way, you make the button "inactive" and it doesn't respond to touches. Even though it is a button, it doesn't appear to be one. This is how Apple expects to add views to a toolbar/navbar as apposed to "float things on top of it". It violates no HIG guidelines, much the opposite, it is a reccomended design technique.
To stop the glow:
Create the button programmatically, make sure it is disabled, add it to the bar, it should then be disabled, but not dim.
In IB, have you tried to select the label and use the "Bring to Font" menu item (under Layout)? It seems like you are trying to do something pretty standard.
When you try to set values, is the label coming up as nil or at address 0x0? It's possible that the label is there, but its text cannot be set because its instance is faulty (not properly connected in IB to the IBOutlet).... Just put a breakpoint on the line where you are trying to set the value(s) for the label, and verify that the label variable is not nil (or 0x0). If it's not, try setting the text and verify on the next line that its text was set properly.
drag a UIButton into your UIToolBar. Then uncheck User Interaction Enables for this button.
Customize your UIButton so that it will look like a UILabel. Hope this will help you.

Putting an NSPopUpButton in an NSToolbar

Having wrestled with NSPopUpButton in a previous question here I am now trying to place a NSPopUpButton inside an NSToolbar. Essentially I want to create something similar to what XCode has by default on the left hand side of it's toolbar. E.g. a pop up button with an action button next to it.
I have seen a method that show's a programmatic way of creating an NSPopUpButton and then adding it to an NSToolbar, but then I can't work out how to do all the Binding stuff that was so handy last time.
Interface Builder hasn't been very helpful, so any help gratefully received.
P.S. Could I solve this by creating a custom view (containing an NSPopUpButton with the usual bindings) and then adding the custom view to the toolbar?
It's actually pretty easy to do what you want here. In Interface Builder, switch to the tree view (the second button on the View Mode segmented control). Expand the window and the toolbar. Then, from the library, drag a popup button onto the toolbar. Interface Builder will embed a new popup button in a custom view for you automatically.
To actually put the button on the toolbar, double click on the toolbar in the window. This will bring up the customization sheet. You can drag the popup button to the desired location on the toolbar.
If you wanted to do this programmatically, you would create a custom view containing your popup button. Then, you'd need to assign it to a new outlet so you can refer to it programmatically. In the toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar method, you would create a new NSToolbarItem per usual, and call setView: to assign the custom view to the toolbar item.
Old post, but note you can also double-click the toolbar in the window and drag straight into the "Allowed Toolbar Items".
You will want to open up the toolbar view anyway so you can drag things in or out of the default toolbar items.