What is the way to make UILabel to wrap text by words properly while number of lines is dynamic ( == 0 )? I tried so far different options and non of them resolve my problem..
I expect to have this:
RANDOMIZE
4
Plain text with number of lines = 0:
Even fixed number of lines doesn't work:
Attributed with Hyphenation = 1:
you can set top ,bottom, trailing and leading constraints of ui label and set number of lines 0.
Hope it works for you.
Related
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
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 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
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 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"