How to put action on text in xcode? - xcode

I don't want to connect action to button, I want when user clicks text that says "Insert" to highlight that text (or change color) and call a function.
How to do that?

UILabel doesn't have a function inherently that you could use, however the UITextField has a property called editing that is called when the user taps the text. This may or may not be what you're looking for because the keyboard is usually open during editing of a text field.
In my honest opinion, it really would be easier to use a button in the long run. It's much less work to put an invisible UIButton on top of the text and check to see if the user taps that.

Related

How to display elements inside NSCollectionView with various shapes

I am a rookie Cocoa guy. I need to design and implement a view which will show collection of labels on Mac OS using Xamarin. These labels will have a text and color associated with them. When shown inside the view, label should expand till it covers whole text and it will be shown with background and foreground colors.
I have attached the picture of this user control on Windows, you can see that labels inside the StackPanel are expanding till they cover the whole text. Hope this gives better idea about my ask.
The $64,000 question is "are these labels controls?" In other words, do you expect the user to click on these to do something, or are they just for display?
If your answer is "just for display", the solution is super simple: Use an NSTextField and programmatically add attributed text (NSAttributedString) to it. Attributed text attaches display properties to runs of text within the field; properties like "background color".
If you want these to be buttons that you can click on, then things get a lot more complicated.
Since you apparently want the button layout to "flow", you might look into imbedding buttons (well, button cells) into an NSTextField using attachments. This is normally how non-text content (say, an image) can be inserted, but with some fiddling it can actually be anything a control cell can draw. See How to insert a NSButton into a NSTextView? (inline).
Warning: this is not a "rookie" topic and will involve control cells and custom event handling.
If I were doing this, I'd probably just create NSButton objects for each label (choosing an appropriate style/look like NSRecessedBezelStyle), create a custom subclass of NSView to contain them, and then override the layout method to position all of the buttons the way I want.
To be thorough, I'd also override the intrinsic size methods so the whole thing could participate in auto-layout, based on the number and size of buttons it contained.

Trying to see the delegate of an NSTextView of an NSCell

I am having a devil of a time trying to figure out how to get the address of the Text Field Editor (NSTextView) of an NSCell—NSFormCell and NSTextFieldCell in particular? NSCell does not have a property to access it. I did figure out the editor is not allocated until one is actually editing the field.
I want to set the delegate so I can capture keystrokes for auto-completion.
By default, there's a single field editor for each window. Even if a control or cell uses a custom field editor, it's still vended by the window. You would call -[NSWindow fieldEditor:forObject:] to obtain the field editor for a given control.
However, the delegate of the field editor is always set to the control on whose behalf it is working. Setting the delegate to something else is likely to break things. So, you would typically use a custom subclass of the control and implement your delegate methods there.
Finally, controlling completions is normally done using -textView:completions:forPartialWordRange:indexOfSelectedItem: in the text view delegate, not by capturing keystrokes.

Remove Focus from UITextField

I'm trying to remove focus from a UITextField and even though I resign it from being first responder, I'm still not able to have the cursor not focus on the text field.
I don't have any other input on the view to move the focus to and I don't want to create a dummy one either. What is a good workaround for this?
As per the documentation.
To dismiss the keyboard, send the resignFirstResponder message to the text field that is currently the first responder. Doing so causes the text field object to end the current editing session (with the delegate object’s consent) and hide the keyboard.
If you call resignFirstResponder on your textfield then it will end the editing session and the cursor wont be focussing on that textfield.
So please verify one more time whether resignFirstResponder is getting called on that textfield which you want to remove the focus.
Please try to set your current class as delegate of your UITextField. I think you forget to set the delegate that's why it's not working as you are expecting.

Editing custom NSCell

I'm in the process of creating a custom NSCell, which will work like the shortcut capture cells in XCode, and trying to figure out how to intercept edit requests (for instance, when a user double-clicks a cell). Is there some way to determine that an edit is being initiated?
Are you looking specifically to detect a double-click in a cell? If so, setDoubleAction:(SEL)aSelector is what you're looking for.
Or is the question more generically about detecting when a cell will enter 'editing' state? In that case, see the table delegate method tableView:shouldEditTableColumn:row:

Hide/disable NSComboBox button

Is there a way to hide the pop-up button of an NSComboBox? I can't find anything in the documentation for NSComboBox or NSComboBoxCell. There is a setButtonBordered: method on NSComboBox, but this just changes to an alterate button style.
If I can't hide it, can I at least disable it?
If the combo box has no items, clicking the pop-up button doesn't do anything.
Maybe you can work around the limitation by emptying the list when you want to disable the button.
It makes clicking have no effect, but it doesn't hide the button or draw it as disabled.
I don't think this is possible. An NSComboBox without the button is effectively an NSTextField, so I guess it was deemed unnecessary. You could probably do this by subclassing NSComboBoxCell and override -drawWithFrame:inView: or -drawInteriorWithFrame:inView:.
Safest way would probably be to add your own buttonHidden property and use the ObjC runtime method class_getMethodImplementation to look up the IMP for the same method in NSTextField and just call that when the button is hidden. You'd effectively be calling super's super, so you'd get a regular text field look.
you can disable it by doing:
myComboBox.Enabled = false;

Resources