1/16/2024 0 Comments Expand cell to fit text xcodeBecause both the label and text field are used to display text, the recipe aligns them using the text’s baseline. This layout also makes the simplifying assumption that the text field is always taller than the label text, and it uses the text field’s height to define the distance from the top layout guide. The rule of thumb from Creating Nonambiguous, Satisfiable Layouts states that we need two horizontal and two vertical constraints per view however, the label and text field’s intrinsic content size provides their heights and the label’s widths, removing the need for three constraints. Notice that this layout only uses two constraints (4 and 5) to define the vertical layout, and three constraints (1, 2, and 3) to define the horizontal layout. You can verify this in the Size inspector. By default, Interface Builder should set the label’s content hugging to 251, and the text field to 250. To have the text field stretch to fill the available space, its content hugging must be lower than the label’s. Name label.Baseline = Name Text Field.Baseline Name Text Field.Top = Top Layout Guide.Bottom + 20.0 Name Text Field.Leading = Name Label.Trailing + Standard Name Text Field.Trailing = Superview.TrailingMargin Name Label.Leading = Superview.LeadingMargin Set the label’s text and the text field’s placeholder, and then set up the constraints as shown. In Interface Builder, drag out a label and a text field. However, you must make sure you have the correct CHCR priorities to get the correct resizing behavior.įor more information on intrinsic content sizes and CHCR priorities, see Intrinsic Content Size. In this example, the label’s width is based on the size of its text property, and the text field expands and shrinks to fit the remaining space.īecause this recipe uses the view’s intrinsic content size, you need only five constraints to uniquely specify the layout. This recipe demonstrates laying out a simple label and text field pair. To view the source code for these recipes, see the Auto Layout Cookbook project. However, using the intrinsic content size often requires setting the view’s content-hugging and compression-resistance (CHCR) priorities, which can add additional complications. In general, the intrinsic content size simplifies the layout, reducing the number of constraints you need. Have a look at other Swift tutorials on this blog and you might find more helpful tutorials that you will like.The following recipes demonstrate working with views that have an intrinsic content size. I hope this short tutorial was of some value to you. Here is how it all looks in my Xcode project. Notice, that I did not need to set width size. Adding the Top and the Leading constraints was sufficient enough. If I run my application now, the label should adjust its width to fit the text. import UIKitĬlass ViewController: UIViewController weak var myLabel: UILabel! Now with the two new constraints added, I can open Swift code editor and set a longer text to UILabel. I will select UILabel on the view and will add two new constraints: Top and Leading constraints. To make UILabel adjust its width to fit the text, I will use auto-layout constraints.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |