How to change Label Font with the FontsPanel in cocoa - macos

I made a PushButton to open up a Font Panel like they did here:Make NSButton open fonts panel
I did all of these through the Interface-Builder by dragging buttons and Labels onto the MainMenu View and i have nothing inside my AppDelegate.h and .m
Im really new to objective c and cocoa so i was wondering if anyone could give me an in depth explanation on how to use that font panel to edit the font in a Label or a Text Field.

Both label and text field are class NSTextField.
The style of the text they display is determined by the attributes on their NSAttributedString.
The Attributed String Programming Guide on Apple's developer site goes into detail on how to handle attributes. The font is one of those inside an attribute called paragraph style.
That should get you started on what to look into.
The Cocoa text architecture is pretty rich and can take some time to get familiar with.
One thing to note, when a text field is in edit mode, it uses something called the field editor for rendering , which is normally a shared instance of NSTextView that is owned by the window and basically appears in front of the text field during editing.

Related

Set global tint in Cocoa applications

I noticed that many apps uses a custom global tint color like Pages (Yellow) or Music (Red). NSAlert default buttons respect this tint, as NSOpenPanel and others do (for example drop-down arrow as show in picture).
I alos noticed that NSAlert brings a delete button that shows text in red. Do you know how to get it to appear?
Thanks
How could I specify a specific global color. I create UI using code and not Storyboard.
Searching in Apple's app folders, such as Pages, I found what we need to do to change the app's overall tint.
Create a color object in the Asset;
In the info.plist add the key NSAccentColorName that is a string assigned to the color name created at step 1;
This colors NSAlert default buttons and all control's accent elements.

Edit text in NSOutlineView like Finder

If you rename a file in the Finder, the text field expands horizontally up to about the width of the column. And then it expands vertically up to three rows before scrolling. I'm assuming this has to be done in a text field outside the outline view. And I can get a text field to resize while I type. I just don't know how to place it over the outline view when necessary. And keep it pinned to the row if the outline view scrolls. Does anyone have any insights? Thanks!
Text editing is handled by a dedicated NSText and it’s called “field editor”. This shared single view is used for all text editing that happens in the window. It’s separate from what usually displays the text (when not editing).
Here are the docs:
https://developer.apple.com/documentation/appkit/nswindow/1419647-fieldeditor
As mentioned in the docs discussion section, you can use and customize another field editor. This should be a starting point for your task.
The window’s delegate can substitute a custom field editor in place of the window’s field editor by implementing windowWillReturnFieldEditor(_:to:). The custom field editor can become the default editor (common to all text-displaying objects) or specific to a particular text-displaying object (object).
NSControl docs also have a section about Field Editor that might help.

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.

Adding controls to NSTextView and binding them to (ranges of) characters

When editing code, Xcode is capabale of displaying in-text controls, like drop down buttons which can show context menu's. I've seen other OS X apps that handle text capable of similar features. See the attached sample.
I presume this effect is obtained using NSTextAttachmentCell - although I'm not sure whether this is the proper way to implement this.
For my own app I would like to use this technique as well.
I have the following questions:
Is NSTextAttachmentCell the correct way to implement such a feature? If not, what would be?
How do I attach a control -comparable to the one in the above sample- to a specific range of text so that its location within NSTextView is dynamic and follows layout actions?
I found this which gives some hints but does not cover the attachment to specific text ranges.
Although NSTextAttachmentCell will work, it has a disadvantage: the cell will become just a glyph in the text which was not what I wanted. It distorts the layout of the text, is selectable etc. I wanted the cell to be drawn over the text, just like the behaviour in Xcode.
The challenge was to find a way of translating a point from a Mouse Moved event to the position of a particular string of characters inside the NSTextView.
After some more digging I found a little gem in Apple's demo apps called LayoutManagerDemo. This demo shows a custom subclass of NSTextView capable of highlighting individual characters, words and lines while the mouse is hoovering its view. From there on it was pretty easy to fade in a button at the required NSPoint and then show a popup menu with some options.

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