UILabel shrink when text changes - ios6

I have a view containing a containerView (gray), which contains a UILabel (blue).
I have layout constraints such that the containerView is always 8 pixels larger than the label on each side, and the containerView's borders left/top/right are fixed. The container view stretches appropriately when the device orientation changes.
The label's numberOfLines property is set to 0. I am setting the label's text in code. The label's height grows to accommodate the text, as expected.
Here's the problem: When I rotate the device to landscape, the label's width grows to fit into the screen's available width, and the text will fit on 2 lines. But the UILabel does not shrink its height to fit snugly around the text. Notice the extra space above and below the text in the label.
So how can I make the label's height shrink with the device is rotated to fit snugly around the text?
I have tried calling [myLabel sizeToFit] in the viewWill/DidLayoutSubviews method, but this had no visible effect.

Check if the label has a constraint that specifies a fixed height: NSLog(#"%#", label.constraints);

Step 1: Subclass UILabel.
Step 2: Set the label's custom class to your label subclass.
Step 3: Override -layoutSubviews in your custom class.
Step 4: In the overridden method, call [self sizeToFit];
This worked for me.

Related

How do I auto resize a label's text in WPF?

I put my label inside a two-column grid and set its size to auto and when I run my application, the label's size grows if I maximize it (I'm sure it grows because I've put a background color and the background color also resizes to accommodate the entire two-column grid it's been placed into. My problem is that the label's text itself doesn't grow and stays the same 9pt. Am I missing a property option to set my Label's text to also auto-resize?
I've read tutorials doing this with a viewbox and a textblock and although it works, I've had trouble setting it up in two columns as it resizes weirdly when I run the application despite setting it up right.
Okay I finally did it. What I did was use the Viewbox + Textblock and I've put a border around the viewbox and set its background accordingly and had the textblock's background to none and now it's working as I wanted it to.
The problem I had back then was I didn't know how to put a background color on the viewbox so I tried a remedy wherein I changed the textblock's background but the problem was the textblock has its own size and even if I stretch it, it would only encompass the width of the text and not the whole viewbox.

Is there any difference between WPF TextBlock and TextBox?

What criteria must I consider when selecting one of these two controls?
Common to both TextBlocks and TextBoxes:
Can be used to display text
Can be set to specific Height and Width or be set to Auto so that they grow in size with the text.
Can set font size, font type, font styling, to wrap and to range left, right or centred.
Can have opacity set and have Pixel Shaders applied.
TextBlock:
Used for displaying text more focused typographically.
Can contain text set to different colors, fonts and sizes.
The line height can also be increased from the default setting to give more space between each line of text.
Text inside a TextBlock cannot be made selectable by the user.
TextBox:
Used for displaying text more focused for content input or when content is needed to be made selectable by the user.
Can only be set to one colour, one font size, one font type etc.
Have fixed Line Spacing.
Can also be set to a fixed height and width but also have scrollbars switched on to allow content to expand.
TextBlock is more lightweight control for displaying text and TextBox is used when you require user input or edit existing text. Proof for mem usage.

WinForms autosize vertically while leaving width adjustable?

Is it possible to use WinForms layout engine to set up a form in such a way that the height is adjusted completely automatically to fit the content, but the width would be adjustable by the user?
As an example, consider the following somewhat contrived form:
TLP: TableLayoutPanel, with 1 row and 2 columns.
label1: Label, contained in the left column. Set Text to something long, AutoSize to false, Dock to Fill.
button1: Button, contained in the right column. Set AutoSize to true.
Now set the row to "AutoSize", the first column to "100%" and the second column to "AutoSize".
Finally, set the whole TLP to AutoSize true, AutoSizeMode to GrowAndShrink.
At this point, the TLP could reasonably be resized horizontally, but WinForms doesn't seem to allow this.
The idea is that the form containing this TLP would grow or shrink vertically based on the width, which the user can change by resizing the window as usual.
Hopefully you have got the solution by now. But in case anybody having a same query, You can set width of Maximum Size Property to the Actual Width of the Form and the Height of MaximumSize to 0.
This is entirely possible. To do this, you have to override SetBoundsCore on your form. In this you can then control the height such that it doesn't change when a resize might want it to. Just store the height you want it to be in a variable and then have your override always set it to that. This way, when you auto-size the height, you save the new height aware before setting form Height and SetBoundsCore will honour that new height.

Panel with percentage coordinates

I would like use a panel whose children have coordinates specified as percentage of total panel's width/height. Moreover, I should be able to animate the coordinate property, for example to make a button move from 10% to 50% panel's width.
I've made 2 attempts:
Use a Grid and specify size as stars - this was not enough, because AFAIK by default WPF cannot animate distance properties specified by stars. I've found somewhere a custom class that enabled me to do so, it even worked, hovewer I consider that solution overly complicated an I am looking for something simpler.
Use a Canvas with fixed width and height and put it inside a Viewbox - this is a simple solution, but when resizing the Viewbox the whole content of Canvas is resized too. I want the content to have fixed size.
Is there a simple solution or should I implement my own panel (or maybe extend one of the existing ones, i.e. Canvas)?
Cheers!
I would:
subclass Canvas, perhaps calling it RelativeCanvas or RatioCanvas
add two attached properties: XRatio and YRatio
override ArrangeOverride and loop over all children. For each child, use their XRatio and YRatio along with the ActualWidth and ActualHeight of the RelativeCanvas to calculate and apply values for their Canvas.Left and Canvas.Top attached properties
You would use it as follows:
<local:RelativeCanvas>
<!-- the top-left of this button will be center of panel -->
<Button local:RelativeCanvas.XRatio="50" local:RelativeCanvas.YRatio="50"/>
</local:RelativeCanvas>
One thing you might like to add after you get that working is control over alignment. For example, I might to align the center of a control to the specified ratio, not its top-left corner.
There's one here: WPF Proportional Panel

How to determine width of a windows forms controls whose width was increased because it is Anchored?

I currently have a Textbox on a Windows Forms, and I need to dynamically add a PictureBox box control at the right of the Textbox.
I create the PictureBox programmatically and I when setting the location of the PictureBox, i'm setting like this:
pBox.Location = new Point(tbControl.Location.X + ctrl.Width, ctrl.Location.Y);
So i'm setting the picture box to be located at the X location of the textbox PLUS the width of the textbox. However, since the textbox has an anchor property set to right, its width increases to fill the space between itself and the form border.
Problem is, that even though the textbox's width is visually bigger than the actual value of Textbox.Width. the Width property is not taking into account the extra width of being anchored.
I already tried properties like Textbox.Bounds.Width, Textbox.ClientSize.Width, Textbox.DisplayRectangle.Width, etc. with no luck. All of those properties return the original Width of the control without taking into account the resized width due to the Anchor property.
Does anyone know how I can determine the real size of the textbox? Thank you
The Width property always tracks the current width of a control, whether it is anchored or not. However, the TextBox is going to grow when you make the container larger and that will make it overlap the PictureBox. You have to anchor the PB to the right as well.
These should be returning the adjusted size. Either you are referring to the wrong textbox, or you are doing the query before the size has actually changed.

Resources