Scale label's font size or rotate label? (swift) - ios

I'd like to fit some text in a label by either adjusting the font size or by rotating the label if the font becomes too small. Using the following, I can shrink the font size down to a certain point (60% of the default in this example), but if that is not enough, any remaining characters that don't fit will be truncated.
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.6
For example, label.text = "This a long label" gets truncated to "This is a long l...". Each label can only have one line of text.
I then want to adjust other labels to the same new font size (or rotate them accordingly).
How can I tell what the new font size is? Or what the actual scale factor is? How can I tell if the text was truncated?

Looks like you can solve this using the following line in your code which would "wrap" the text to fit the field, and not give the "..." and display the entire text:
label.numberOfLines = 0
Other than that, I'm not sure of a way to automatically change it, but perhaps you could use an "if" "then" statement to say something along the lines of:
"If" label.numberOfLines = <1, "then", label.font = UIFont.boldSystemFont(ofSize: 42)
Put a lesser and lesser "ofSize" amount to until the text fully fits in the field.

Related

Calculate how many chararcters can fit on one line of a UILabel

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();

How to dynamically calculate the number of characters a UILabel holds before it gets truncated?

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 😜

Auto size UILabel text

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.

Replacement for minimumFontSize for a UILabel

I'm aware of minimumScaleFactor but it isn't useful for the result I'm after.
I have two UILabels and I want them both to be the same size. I want them to fit to the label's view if possible down to a minimum size and then I'll do a check to set them both to the smallest font size, so that they both fit and are both the same size.
I don't think I can achieve this using minimumScaleFactor because it doesn't seem to actually change the font size, it seems to use some sort of scaling on the view (correct me if I'm wrong). I don't seem to be able to set this scale manually other than setting it's minimum value so it seems to be useless in my circumstances.
Set max_msg_height that you want in your cell. This function will check if your message length is more than max_mgs_height and font size and if it is greater than 12 points, it will continue. In my case i have set max_msg_height = 160. Default font size to 16. So, if I got message which is not adjust in lable frame then I reduce font by 1 point by using this while loop as shown below:
while (messageLbl.frame.size.height > max_msg_height &&
messageLbl.font.pointSize>12) {
messageLbl.font = [UIFont systemFontOfSize:messageLbl.font.pointSize-1];
[messageLbl sizeToFit];
}

UILabel text is not displaying properly

I want to show the text in uilabel. The text is ,000124520.061112782.,3299985343, I have assign line break mode to character wrap and number of lines to 0. But in label first line is displayed only the , and then other characters are displayed in next line. But i want to display the text in two lines only. But it is displayed in three lines. I don't know why , is displayed in first line. Please anyone help me.
You should set the numberOfLines of your UILabel to 1.
When the numberOfLines property of a UILabel is set to 0 the UILabel will render its text over as many lines as it needs - e.g. its infinite. To fix your text to a single line you should therefore set the property to 1.
Obviously your text may not all fit on a single line so you should also set the UILabel's minimumScaleFactor property to allow the UILabel to adjust its font size downward (to the supplied limit) to fit the content into the frame of the label. You will also need to set the UILabel's adjustsFontSizeToFitWidth property to YES.
Further to the above an easy way to calculate the value for the minimumScaleFactor property is to divide the minimum font size by the maximum font size. So, say for example your label's default font size was 12 and you were happy for it to drop to 10, then assign the minimum scale factor as follows:
myLabel.minimumScaleFactor = 10/12.0f;

Resources