NSParagraphStyle how to set maximum number of lines per paragraph - ios

I am trying to achieve something like this (with the grey text always at the bottom of the textview):
but when I set the line break mode to NSLineBreakByTruncatingTail it only allows 1 line of text per paragraph like this:
I would like to truncate the first paragraph to a maximum of 2 lines and have the grey text always as the last line of text.

Related

Multiline truncate middle is not working in swift

I have a label inside stack view with fixed height and width, the number of line is set to 2 and line breaking mode to "truncate middle". However the label's text gets truncated at it's tail. If I have single line it is getting truncated in middle. Does attributed text restricts the middle truncation?
#Shreeja
step 1 -> set number of lines to 0
step 2 -> set line break word wrap
Step 3 -> Remove Fix height
Happy coding :P

When should you use lineHeightMultiple vs. lineSpacing?

There seem to be two different ways to explicitly set the line height of an attributed string in iOS. Which is the preferred method and why?
https://developer.apple.com/documentation/uikit/nsmutableparagraphstyle/1524596-lineheightmultiple
https://developer.apple.com/documentation/uikit/nsmutableparagraphstyle/1524596-lineheightmultiple
Note that there are subtle differences to using these parameters, even if you are effectively setting the same distances.
The above shows lineSpacing applied. When selecting text, the spacing is visible below the lines of text. The last spacing is truncated.
The above shows lineHeightMultiple applied. The selection shows the spacing is above the lines of text. The top spacing is NOT truncated.
Line Spacing is the distance in CGFloat between two lines.
Line Height Multiple the space between each line equal to the value multiply with the line height. Imagine Line Height Multiple = number of new lines for each line.
ie: lineHeightMultiple = 2 will insert 2 "\n" and lineHeightMultiple = 3 will insert "\n".

iOS: UILabel How to achieve TextAlignment.justified on one line

How to achieve TextAlignment.justified on one line of a label.
I have one UILabel of fixed width (200)
Number of lines of this Label is always 1
I want the text of this One Line to be justified on both edges
So far What I have seen the TextAlignment.justified is only applied on paragraph and also on paragraph it is not applied on last line or single line.

How to compress the text in a line to fit it?

I have a whole paragraph and I want to fit it in a label and be justified.Also, I want every line to have a specific range of words. I know that there is "Autoshrink" in Xcode, but it does not seem to work because I am trying it in a multiline text.
Here is an example of what I am trying to do http://imgur.com/Kxbs64Q. You can see how the text is fit into the label and is justified. However, not every line has the same number of characters. Is there a way to compress the text inside the label in swift?.
Use a minimumFontSize for your label. It will allow you to fit all of your text in the label. Also, set your label's numberOfLines to 0 if you want label to automatically create multiple lines. Use this link for help.

Get text of UILabel after replacing the wrapped text with line break

I'm working in an app to draw text on UIImage. I display the image on screen and a UILabel on it describing the text. User can edit the text. I set lineBreakMode to UILabel to be WordWrap. Now when I get the text to be drawn on the UIImage to export it as an Image. The text drawn as one line although it was displayed as multi line on the screen because of the wordwrapping.
Is there a way to replace the wrapped works with linebreak "\n". For example "Lorem Ipsum" when I display it in small size label, "Ipsum" will be drawn in new line. So I need I call getText, I get it as "Lorem\nIpsum"
It sounds like the problem is the number of lines you have. I had a similar problem before where the words didn't cut off. Add this line of code to your project:
label.numberOfLines = 3
You can set the number of lines to whatever you need. It should now put each word on a new line. Cheers!

Resources