5

What is the swift Equivalent for 'text-align-last' css property? I prefer a codeless solution.

Here is what i have done and what i get:

enter image description here

The last line (sometimes the only one) is aligned to the left, which is inconvenient.

Dorad
  • 2,533
  • 1
  • 35
  • 58
  • So you want to make the text fill the width but align the last line. Actually I haven’t seen that before so I would suggest that if you really need it then add a web view and put a basic format to the text – zombie Jul 20 '17 at 07:00

2 Answers2

2

You need to change the label's text from Plain to Attributed, then you can paste any string and the alignment, as well as other attributes, will hold. So basically any text style that can create on word processor application can be used here.

In the following example I've used pages (Mac application) to edit the text format as I liked and copied it to the label text box in Xcode.

enter image description here

Here is a picture of the simulator running the app: enter image description here

inspector_60
  • 418
  • 1
  • 3
  • 12
  • Sorry, but i think you have missed the point. I have asked on how to align a label to both sides (justified) while aligning the last line to the right, as opposed to the default behavior which aligns the last line to the left. – Dorad Jul 20 '17 at 04:17
  • I've edited my answer, hopes this helps, the key here is that with attributed string any style can be achieved. – inspector_60 Jul 20 '17 at 06:38
  • Thanks, I have tried. It does look good in the storyboard. However when thee app is running in a simulator the label is is still "leftist". :-( – Dorad Jul 20 '17 at 07:21
  • Check out the picture that I've add in my answer, check that you have the same settings as my example and make sure you didn't add spaces earlier that are still there – inspector_60 Jul 20 '17 at 07:31
  • also click on the "three dots and a small arrow" button (to right of the a marked button on my answer) check that the spacing there is ok – inspector_60 Jul 20 '17 at 08:30
0

While it is not possible to apply different aligns on a single label, if the last(and sometime only) line should be aligned to the right why not align the entire label to the right?

enter image description here

EDIT

If that doesn't fix you problem then I don't think it's possible to resolve without code.

In the case you do decide to write some code you could look into determining which is the last line of your string (maybe: How to get text from nth line of UILabel? ) and try to apply different formatting with AttributedString.

If that works then you can always subclass UILabel and override func layoutSubviews() to calculate this automatically for you. This way you won't have to think about it again!

Thomas
  • 1,702
  • 14
  • 26
  • Because when the text is fully right aligned it doesn't spread in the whole horizontal space. (CSS allows it, Microsoft Word too) – Dorad Jul 11 '17 at 08:37