text-align-last css property Swift Equivalent

2019-06-21 19:28发布

问题:

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:

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

回答1:

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.

Here is a picture of the simulator running the app:



回答2:

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?

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!