UIButton resizing to fit text length - xcode

There are a number of posts on Stack about this subject but I can't get any to work.
I have a Button whose text literal is supplied by a variable which can change in length. I want the button to be sufficiently long so the text is readable rather than concatenated in the middle with ".....".
If I use something like
CGSize stringsize = [myString sizeWithFont:[UIFont systemFontOfSize:15]];
NSLog(#"Width = %f", stringsize.width);
[myButton setFrame:CGRectMake(20,0,stringsize.width, stringsize.height)];
Similarly I have been unable to get commands like to work either.
[self myButton sizeToFit];
Does anyone have a solution that does work?
I am trying to implement these from viewDidLoad.

Are you using a storyboard? If so, go to your file inspector in the storyboard and see if you have "use autolayout" checked. From what I can tell, uibuttons won't respond to frame changes while this option is checked.

Answer for Xcode 7.2
Click on the view that needs the fit
(Not from the list of views from the left panel, but in the stage itself)
Now click on Editor menu, and then Size to Fit Content
Voila!!
You can use the shortcut command + =

Related

Prevent UIButton automatic resize after changing text in Interface Builder

If I slightly modify the title text of a UIButton I've added to my view in Interface Builder it automatically resizes the button. This is really annoying if I only make a small change yet have to constantly resize my buttons. Does anybody know of a what to stop this from happening?
You can avoid this by assigning a constant-sized frame to your button programmatically. Make sure your button is already created from the XIB/NIB file (i. e. it is not nil and it won't be touched anymore by the NIB/XIB) at a point in the code, then add this line:
myButton.frame = CGRectMake(x, y, width, height);
where x, y, width and height are the desired/expected position and size of the button, respectively.
I'll answer my own question. Instead of directly editing the text inside the label, do so by changing the text in the Title field inside the Attribute Inspector (select the UIBUtton label, then open the inspector tab which is the 3rd icon from the right on the right-hand panel of IB).

Vanishing NSTextField Text

I have a custom view in a .xib file, which I use as the contentViewController for an MAAttachedWindow. The view has several NSTextFields in it.
When I open the MAAttachedWindow first time, everything is fine. Text shows up in all relevant text fields. Then, if I close the window (which sets it to nil) and then call it again (which reinitializes, using the same custom view as the contentViewController), the last firstResponder text field is now blank.
The strange thing is that if I click the "empty" text field, it shows the correct text. This can be edited, and behaves appropriately as long as this text field has focus. As soon as something else becomes firstResponder, the text vanishes again.
Updates:
Changing the color did not change the aforementioned behavior.
The text color does not change at any time during this process.
Placeholder text also is subject to the aforementioned behavior.
No errors are occurring at any time during this process.
This does not happen to NSSecureTextFields.
I first encountered this problem about 5 years ago with accessory view of a NSSavePanel.
The solution that I've found was to move the first responder to the panel itself, before it's closed. Here's my exact method:
- (void)windowDidEndSheet:(NSNotification *)notification
NSSavePanel *savePanel = [(XSDocument *)[self document] savePanel];
if (!savePanel)
return;
// this fixes a bug where on next opening one of accessory view's text field will be blank and behave strangely
[savePanel makeFirstResponder:savePanel];
}
Try changing color of textfield text to red color (or any other color) you may get what happens here.
I got it!
I simply needed to explicitly remove the viewController from its superview before closing (and subsequently deallocating) the MAAttachedWindow.
Try resigning all first responders before setting the window to nil.

How to change the height of an NSWindow titlebar?

I want to change the height of an NSWindow titlebar.
Here are some examples:
And…
I could use an NSToolbar, but the problem is that I can't place views very height (For example: I can't place the segmentedControl higher than in the picture because there is still the titlebar)
If I remove the titlebar I can't place a NSToolbar and the window isn't movable.
Have you any ideas?
This is much easier than one would think. I too went on a quest to do something similar for my app.
Real App Store app:
My App Store app look-alike:
No disrespect to INAppStoreWindow, it is a very good implementation and solid. The only draw back I saw from it though was that there was a lot of drawing code along with hardcoded settings for the TitleBar colors which Apple can adjust at anytime.
So here is how I did it:
A) Create a standard window with a Title Bar, Close, Minimize, Shadow, Resize, Full Screen - Primary Window all set.
Note: You do not need a textured window nor should you set a title
B) Next add a standard toolbar with these settings:
Icon Only
Visible at Launch - ON
Customizable - OFF
Separator - ON
Size - Regular
Remove all the Toolbar Items and add only these in the following order
NSSegmentControl (51 x 24) -- | Flexible Space | -- NSSearchField (150 x 25)
C) In your content View directly under the toolbar add a regular sized NSButton set like so:
Bordered - OFF
Transparent - OFF
Title -
Image -
Position - Text below the button
Font - System Small 11
Ok, pretty easy so far, right?!
In your Window Controller or app delegate....
setup IBOutlet(s) to your NSButton(s)
Note: Remember to hook up your IBOutlet in interface builder
Ok don't be scared we have to write a tiny bit of code now:
In awakeFromNib or windowDidLoad....
Get the content views' superview (aka NSThemeView)
Remove your button from its superView
Set the frame of your button
Add the button back to the theme view
So the code would look similar to this:
NSView *themeView = [self.contentView superview];
NSUInteger adj = 6;
[self.btnFeatured removeFromSuperview];
self.btnFeatured.frame = NSMakeRect( self.btnFeatured.frame.origin.x,
self.window.frame.size.height - self.btnFeatured.frame.size.height - adj,
self.btnFeatured.frame.size.width, self.btnFeatured.frame.size.height);
[themeView addSubview:self.btnFeatured];
That's it! You can use your outlet to enable/disable your button, setup a mask image when selected, enable/disable the toolbar or even hide everything and add a window title. All of this without worry if Apple changes their standard Window Titlebars.
P.S. No private frameworks were used in this posting whatsoever!
INAppStoreWindow is a NSWindow subclass, it tell you how to change the height of title bar.
https://github.com/indragiek/INAppStoreWindow
http://iloveco.de/adding-a-titlebar-accessory-view-to-a-window/
This example tells you how to add buttons in the title bar.
You'd have to subclass NSWindow and do a custom window frame drawing. It's not only about a titlebar. It's about whole window frame (so you can, actually, put close/minimize/zoom buttons at the bottom if you wish).
A good starter is at "Cocoa with love" website.
There are a few new solutions based on INAppStoreWindow and without warning and log message, for anyone who wants to change the height of NStitlebar, change the position of traffic light, add an item(e.g. a NSbutton) on NStitlebar and change its position, please check below.
WAYWindow:
https://github.com/weAreYeah/WAYWindow
NStitlebar_with_item:
https://github.com/ZHANGneuro/NStitlebar_with_item

Dynamically lay out a Window in Cocoa using Core Animation and populate it

What I'm looking for is a way with CA to dynamically lay out a window. Imagine the following SQL query in a window, each name between +PLUSSIGNS+ being a NSPopUpButton, rest is static text.
Select *
from +BURRITOS/TACOS1+ +AND/OR1+
+BURRITOS/TACOS2+ +AND/OR2+
Where
+TOPPING1+ +EQUALS/LT/GT1+ +TOPPINGLIST1+ +AND/OR3+
+TOPPING2+ +EQUALS/LT/GT2+ +TOPPINGLIST2+ +AND/OR4+
Ok: So the window starts showing "Select *" and "from" plain text labels, and BURRITOS/TACOS1 selected to "--" instead of a valid value.
When I set BURRITOS/TACOS1 to a valid value (BURRITOS), I want the AND/OR1 NSPopUpButton to appear, selected to "--". I also want the "Where" label to appear and I want "TOPPING1" "EQUALS/LT/GT1" "TOPPINGLIST1" to appear. All 3 of those will be selected to "--".
When I put AND/OR1 to a valid value (AND or OR), I want BURRITOS/TACOS2 to appear. If I select that to a value, I want AND/OR2 to appear. If I set that to a value, I want BURRITOS/TACOS3 to appear ....
If I set TOPPING1, EQUALS/LT/GT1, and TOPPINGLIST1 to valid values I want AND/OR3 to appear (as "--"). If I set AND/OR3 to a valid value, I want TOPPING2, EQUALS/LT/GT2, TOPPINGLIST2 to appear. If I set them to valid values, I want AND/OR4 to appear...
If for instance AND/OR3 is set to -- and there was a line under it, I'd want that entire line to disappear.
At the bottom of the entire Window I need a static checkbox "enable", always appears. I also want a left and right arrow button - clicking left would make the entire window "flip" to the left. Clicking right would make the entire window "flip" to the right to new queries.
I'd like these new NSPopUpButtons to appear similar to Mail.app where a new text entry for CC BCC etc appears based on your settings using that picker control thing.
Ends up this is truly 2 questions.
1 - Dynamic layout of window. In simplest way this is done by putting an NSView in an NSWindow and then using NSView's addSubview.. example:
NSRect rect = NSMakeRect(0, y, 100, 10);
NSButton *button = [[NSButton alloc] initWithFrame:rect];
y += 15;
[topView addSubview:button];
Here an NSButton is put every 15 pixels inside the view. Note that y has to be maintained by me, it's not automatic. If we overrun our NSView bounds we have to manage that size ourselves as well.
2 - Animating this transition. Using Core Animation isn't that smart, the more sensible route is NSAnimation and specifically NSViewAnimation. There's a great example thanks to Apple here. For my purpose I need to use this to resize and also move NSViews. Also If I want a Button to "fade in" what I can do is copy the NSView, keep that as "old", modify my NSView, and fade between those.
...thanks to #cocoa on freenode..

Creating a checkbox programmatically using Cocoa

I need to create a checkbox programmatically in Cocoa and when I try and make a button with buttonType set to NSSwitchButton it displays the title, but does not show the button as a checkbox. I think I am missing something but I can't find any resources about making things like checkboxes without using the Xcode GUI.
The question is a little old so you've probably already figured it out, but I found it while searching for this exact thing. Alex danced around the solution without actually providing it. So here, for Google and all mankind: how to programmatically create a checkbox in Cocoa.
NSRect frame;
frame.size.width = frame.size.height = 18;
NSButton *myCheckBox = [[NSButton alloc] initWithFrame:frame];
[myCheckBox setButtonType:NSSwitchButton];
[myCheckBox setBezelStyle:0]; // This is unnecessary. I include it to show that checkboxes don't have a bezel style.
[myView addSubview:myCheckBox];
I don't think buttons are bezeled by default when created programmatically. Check the setBezelStyle: method, as well as setBezeled: and setBordered:. One of those should give you what you want.
I had failed to execute setImagePosition properly and this was causing the checkbox not to display.

Resources