floattext of a TextField overlaps with initial text content instead of floating up (again) - gluon

I am reporting another regression bug since this original post 5 years ago:
floattext of a TextField overlaps with initial text content instead of floating up
As a workaround, when I view a form that may or may not have data prepopulated in it, I check if the String I intend to set into the Textfield is empty. If so, then I append a single space in it. This way, as the User visits the same View of different records, the Textfield's floating text will always be floating above instead of overlaying possible form data text. But this means, even if the Textfield seems empty to the User, the float text is still a label over the Textfield every time.

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.

How can I make a UITextField with padding on the left of the content?

UITextField shows its content text bang against the left edge of the field, which is ugly and can be hard to read if the field has a visible border. UITextView, on the other hand, automatically has padding (blank space) around the text. I examined and experimented with all the UITextField properties but I don't see any way they can be used to get padding around the text.
I also tried calling drawText(in:CGRect) on the text field but it had no effect. But I'm not sure what CGRect is needed in that call. I tried different border styles including .none. I also tried making a blank UIView and assigning that to the text field's leftView property but that also had no effect. And I tried doing these things in viewDidLoad, viewDidAppear, and at the point where I'm writing the text into the textField. The text shows in the textField as expected but none of these other things seem to affect the textField's appearance in any way. It seems to me lots of people must show text in a UITextField and somehow get it padded on the left so I'm misunderstanding something but what???
The following works for a field called "textField". It inserts padding by shifting the left side of the field's content 8 pixels right relative to the left edge of the textField:
textField.layer.sublayerTransform = CATransform3DMakeTranslation(8, 0, 0);
This is given in an answer to a different question by user Dhiru (reputation 1285) in Nov. 2016. Thank you Dhiru!
I have to add that I am not familiar with either sublayerTransform or CATransform3DMakeTranslation so I can't be sure that this doesn't have some side effect that isn't immediately obvious. I'm a little worried that it's called "3D transform" whereas there is nothing 3D about my application. But at least for now this seems to answer the question.

OS X bindings: UI element isn't committing binding. Why is that?

Look at the simple app screenshot below. There is a text field and a segmented control. There are three bindings in play:
textfield value binding goes to NSUserDefaultsController values.TextValue
textfield alignment binding goes NSUserDefaultsController values.Alignment
segmented control selectedIndex binding goes to NSUserDefaultsController values.Alignment
So the segmented control controls the text field's alignment.
Problem:
When the text field has first responder, if you click on the segmented control to change the alignment, the text field 1.) throws out the value being edited and reverts to the last bound value, 2.) does not update its alignment, 3.) remains first responder.
Expected behavior would be: when you change the segmented control value the text field resigns first responder, commits the value in the UI to user defaults, and updates its alignment. How can this be done?
You’ve hit a bug and should report a RADAR. There have been many bugs with textfields and bindings when you’re editing a textfield, it’s not an area the engineers focused on originally.
You don’t even need to bind the textField’s value to replicate this, you can just bind the alignment of the textField.

Placeholder Adopting from Previous Text Field

Quick question! When any NSFormCell becomes the first responder, its placeholder text becomes that of the last NSTextField to have focus. Here's an illustration: http://i.imgur.com/3dlzj.jpg
However, the NSTextFields never encounter this! Have you seen this before? This is happening in Xcode 4.3 — what thoughts do you have?
Thank you!
This isn't an answer, but just some further observations.
I did some experimenting with this, and it's not anything you're doing. If you just drag a form and a text field into a window in IB, and put a placeholder string in the text field, then that placeholder appears in the form if you first select the text field and then one of the form cells (with no code at all in the app). It doesn't matter whether you put any placeholder in the form in IB, it still gets the one from the text field. I also put a button in the window with an action that logs the placeholderString of one of the cells -- it always logs null, even though it shows the text field's placeholder. If you put a placeholder in that cell (in IB), then it logs that placeholder, but if you select the text field and then that cell, the text field's placeholder shows up, but the log still shows the original placeholder.
This looks like a bug to me. It might have something to do with the field editor? Isn't there a shared field editor for all text fields in a view?

How can I get an NSTextField to behave like a HTML text?

I have some static text that is usually only 1 line long that displays in a NSTextField in my xib. In some instances, the text is long enough to warrant 2 lines, and I just want the label to resize vertically to fit it, without giving me scrollers or any thing else. Think of how text on a webpage behaves… that is what I want. I just want the label to grow shrink with different text, and with adjustments to its width. How can I achieve this?
UPDATE
Here is a video of how it currently behaves: http://screencast.com/t/4JYTv7jVG3O
Notice how when the NSTextField is two lines long, there is a big gap underneath the text. This is because the stars and button are aligned at the bottom of the frame, and because I have to have the frame taller to accommodate 2 lines, they stay there. If I can get an answer to this question, I would make the frame shorter for the 1 line text, and make the bottom textfield (with the smaller text) taller to compensate. Can this type of floating layout be done?
One option is to actually use a WebView to display your content. You will then get exactly the behaviour you are expecting, at the cost of a bit of work to manage interaction with the controls.
You would need to set the WebView to display no background, using [webView setDrawsBackground:NO].
You'd also need to construct the content (including the star rating and the button) using HTML/CSS and then use the Objective-C/JavaScript bridge to call back to your app when the button is pressed.
More information on calling Objective-C from JavaScript is here.
You could probably also use an NSTextView and embed the button and star rating as NSTextAttachment objects but this is quite complex, it would be a lot easier to use a WebView.
The only other alternative that I can see is writing a view controller that manages the layout of the controls based on the current size of their container view. You would need to measure the text to do this and one way to do that is to use the excellent NS(Attributed)String+Geometrics category written by Jerry Krinock.

Resources