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

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.

Related

uitableviewcell dynamic label width with VFL

I need to fit a label and an image in a table view cell in such a way that the image should be placed immediately next to the label but within the cell visibility. ie., they should look like below,
When the text is smaller or within a line, then the image could be immediate next to the label. Eg -
Smaller text (image)
When the text goes beyond a line, then image could be at the edge of the cell because the label length had already been extended to the length. Eg -
Multiline text multi-line text multi-line
text multi-line text multi-line text multi (image)
multi-line text multi-line text
I am trying to achieve this using VFL and no storyboard or nib.
What I tried out :
1. "H:|-[label]-[image(==20)]-|" , "V:|-[label]-|"
This makes the image to always attached to the edge thus the case 1 fails which needs the image near the label. Eg -
case 1 :
Smaller text (image) (not ok)
case 2 :
Multiline text multi-line text multi-line
text multi-line text multi-line text multi (image) (ok)
multi-line text multi-line text
2. "H:|-[label]-[image(==20)]" , "V:|-[label]-|"
This makes the image near the text thus okay for case 1 whereas that fails for case 2 which does not wrap the text to multiple lines then and the text is extending beyond the cell width. Eg -
case 1 :
Smaller text (image) (ok)
case 2 :
Multiline text multi-line text multi-line text multi...
(not ok - text not wrapped to multiple lines, remaining text and image hidden)
How to resolve this? Setting priorities to some constraints would help ?
I don't have much experience with VFL but , I know Autolayout so I can give you Idea how to resolve
I have seen your both tries
1. "H:|-[label]-[image(==20)]-|" , "V:|-[label]-|"
2. "H:|-[label]-[image(==20)]" , "V:|-[label]-|"
In first try you have fixed image on trailing
on second try you have fixed image with label
What would be combine solution ?
Your clear requirement says that you need your image attached with label trailing also take care about tailing to superview
So clear solution for that problem is
you have to give your imageView Trailing may be this code [image(==20)]-| should be changed with Greater than or equal to relation with 0 Constant
it will tells autolayout engine to keep Min 0 Distance from trailing of superview
Sorry I couldn't give you exact code as I don't have much experience with VFL
Hope it will help to you

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.

NSParagraphStyle how to set maximum number of lines per paragraph

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.

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!

NSAttributedString alignment to particular text

I m trying to align the attributed text of UILabel with multiple lines as below.
The alignment should be like all the days in chars left align and timings right align. How can i do this .Please suggest.
Thanks
I would break the string into 2 and align the day to right and the time to left. Otherwise you need to calculate each letters width (and the spacing between the letter before and/or after.) Fonts are complex as the glyph itself and its spacing to neighbours are different from letter to letter.
I hope there is someone who found an elegant way to do this.
I also had very similar task and here is how I did it.
I achieved this by breaking the line into two pieces.(what should be aligned left and what should be aligned right)
Then on your view create two UILable with the equal widths.
After that display left side in Keys Label and right side in Values label
Do not forget set Lines to 0
Left align your text.
textAlignment = NSTextalignmentLeft
Separate each line with "\n" to force a new line
#"Tuesday 9:20\nWednesday 8:40"

Resources