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".
Related
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.
I need to truncate some multi-line labels with ellipsis. However, setting
mylabel.lineBreakMode = .ByTruncatingTail
mylabel.adjustsFontSizeToFitWidth = false
reduces line spacing. Is there a way to keep the original line spacing?
Ended up truncating manually by whitespace which has two advantages:
words are not broken in the middle, and
sizeToFit sets height more precisely
(I need not-more-than certain height and the label has shadow so exact height is unwanted).
I'm trying to stop text from wrapping when it doesnt fit in a line of a multi-line (numberOfLines = 0) label but to do that I need to know how many characters can fit in a line of the label so I can cut off the rest and just use "\n" to add the next string to the next line. How would I go about finding how many characters can fit on each line with my font and size and size of the label (Courier 17.0 monospaced by the way). Also the size of the label can be dynamic since like screen size/size classes can change, which is why this is needed.
Ex of what I want to happen and why I need to know how many characters fit
on a line:
var lineOne = "Too long"
var lineTwo = "Also too long"
myLabel.text = lineOne + "\n" + lineTwo
// Then I would calculate how many characters can fit on a line and alter the two strings accordingly
Output of the label (pretend the label character limit happens to be like 3):
Too
Als
I Just realized another thing I need to be able to calculate is how many lines can fit in a label (height) if the numberOfLines = 0.
I don't think you need to do them programmatically..
To do that, Select your label and in the Attributes inspector, change the
Line Break to Character Wrap.
Then change Lines to the maximum amount of lines you want to allow.
And you can add constraints for height and width accordingly. Then use <labelOutlet>.sizeToFit();
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.
I have 2 UILabels, one which is multi-line and other is of 2 lines. I always have an image besides these labels and its becoming difficult for me to align the image with the text. Eg:
If the 2 line label just has some small text on it, the text is displayed on the middle of the label and the image is aligned with the first line. If it has exactly 2 lines on text on it, then the image is aligned to the first line of text which is exactly what I want.
Is there a way I can get the position or co-ordinates (x,y) of the first line on UILabel so that I can adjust the image constraints accordingly then?
This will give you the co-ordinates and the dimensions:
label.frame.origin.x
label.frame.origin.y
label.frame.size.width
label.frame.size.height