12/27/2023 0 Comments Expand cell to fit text xcode![]() ![]() The system then selects a constraint to break, and either the text field or the label is truncated. If the name label’s text or font is large enough however, there won’t be enough space, producing an ambiguous layout. In this example, modifying the compression resistances is left as an exercise for the reader. ![]() The compression resistance defines which of the views should be truncated when there isn’t enough space. If the layout might be displayed in a space that is too small for the controls, you need to modify the compression resistances values as well. Because this is larger than the text field’s default 250, the text field expands to fill any additional space. In this example, Interface Builder should already set the name label’s horizontal and vertical hugging priority to 251. You do this by modifying the view’s CHCR priorities. Horizontally, you still need to define which view should expand to fill the available size. Because 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. The following recipes demonstrate working with views that have an intrinsic content size.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |