mihi
mihi

Reputation: 47

two labels horizontally in tableview?

enter image description here

I would like to have two labels in my tableview. but the label resize option is disabled. How to have the two labels horizontally?

Upvotes: 2

Views: 721

Answers (5)

RSR
RSR

Reputation: 636

if you want a easy and quick fix for it, implement a stack view inside the tableview cell. You can change the stackview as you want and also you dont have to worry about applying constraints.

Upvotes: 0

Krunal
Krunal

Reputation: 79646

You may need AutoLayout and Custom Tableview Call. Use two different labels, e.g. one for username (left aligned text) and another for garbage text (right aligned text).

Now, set Content Compression Resistance Priority for one of both label, according to your requirement for data visibility when there is long texts in user name.

Look at these snapshots:

Labels with default content compression resistance priority

enter image description here

I changed content compression resistance priority for label blablabla blablabla, from 750 to 749.

Result is:

enter image description here

For more details see Apple document: Setting Content-Hugging and Compression-Resistance Priorities

Upvotes: 1

Pratik Patel
Pratik Patel

Reputation: 1421

Are you sure you have selected Label?

If you know about Layout constraints, it will be easier than this auto resizer.

Label 1==> Set Leading, Top & Bottom constraints to SuperView. Set Trailing constraint to Label2.

Label2==> Set Trailing, Top & Bottom constraints to SuperView. Set Trailing constraint to Label2.

Now, after this, it will show Red error arrow. Now, you have to decide which Lable width is a priority. After deciding, select that Lable and set Horizontal Content Hugging to 251(High) and Comprehensive to 751(High). Also, change lower priority label Content Hugging and Comprehensive to 249 and 749 respectively. Now, Red error arrow will not be shown and in the cell it will show all text in both labels without and cut.

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

Drag a horizontal UIStackView from object library and set it's constraints like this inside the cell

enter image description here

//

drag 2 labels inside it and make the distribution .FillEqually

// enter image description here

Upvotes: 0

AtulParmar
AtulParmar

Reputation: 4570

Select UIlabel instead of the cell and then you can set Auto resize like follow.

enter image description here

Upvotes: 1

Related Questions