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();
Related
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.
For example, I have the following two string: "How Munched is That Birdie in the Window?" and "S22 - E7". I want to present in the label the following: "How Munched is That Birdie in ... S22 - E7" If the string is too large according to the label's size and doesn't fit it". How you can see, the three dots are placed always in the first string, the second string is always shown full.
How can I achieve this?
Here is how to do it.
yourlabel.adjustsFontSizeToFitWidth = false
yourlabel.lineBreakMode = .byTruncatingMiddle
You can set the UILabel's ParagraphStyle LineBreakMode to byTruncatingMiddle, which will probably work in most cases with carefully planned label size. From the docs:
The line is displayed so that the beginning and end fit in the container and the missing text in the middle is indicated by an ellipsis glyph. This mode is used for single-line layout
If you want to guarantee none of the "S22 - E7" string is truncated, you would have more control using two labels and setting layout constraints such that the width of the protected label is preserved so that it can display the full string whenever possible, but that is probably overkill in most cases.
You may also find this answer helpful if decide to go a different route by manually manipulating the displayed string based on detecting how many characters will be visible given the width and font.
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.
I have a UILabel that is configured with dynamic parameters:
textLabel.font = UIFont.systemFontOfSize(some_dynamic_size, weight: some_dynamic_font_weight)
textLabel.numberofLines = some_dynamic_number_lines
textLabel.frame = CGRectMake(0, 0, dynamic_width, dynamic_height)
How to calculate the number of characters this UILabel can hold before it gets truncated?
Calculate how tall the label would want to be given its font, text, and width. If that is taller than you want to make it, the text won't all fit.
The issue is that your label will be able to fit more or less text given on the font that you use and the size that it is set too. If you set that to a standard size say for instance San Francisco at 12pt then you will still have a varying amount of letters that can fit because not all letters are the same width for instance 'WWW' is allot bigger than ' lll ' but you could determine a "best estimate" by taking the average letter width. Then dividing the size of the label by that then you would have a general idea of how many letters could fit but you also need to account for the '...' that is inserted.
But I would suggest not concatenating at all and just make the text a variable size so it can shrink and show the whole word. I suggest looking into 'dynamic text' but I don't know what your application is so that may not be the best suggestion. Hope that helps 😜
I have a UILabel with multi lines. I'm trying to get the text to auto size, and fit on the line it's in. So instead of:
longe
st
The text size should become smaller so that it can fit on the same line:
longest
Note: I only need it for the first line.
I tried the following:
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor=3.0;
That didn't work, so I tried label.minimumScaleFactor=3.0 / 9; (9 is the font size.) That also didn't work.
I have a UILabel with multi lines
Well, that's the problem. The automatic size adjustment feature of UILabel works only for 1-line UILabels (i.e. numberOfLines must be 1).
You might be happier with two labels, one being a single-line UILabel for the first line which can shrink its size, the other for the remaining lines.