UILabel text is not displaying properly - ios

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;

Related

Why UILabel's height become zero if doesn't hold any text?

I expected from the label to keep the minimal height of one line even if you remove the text out of it.
Here's the picture:
How it should look (but without whitespace):
If I enter the one whitespace in it, its height gets corrected. But I dont want to put whitespace into empty labels. there should be a better solution.
The UILabel control have an intrinsic content size so if you don't have any text his height will be zero, If you need a min height then you must define a Height constraint with >= your minValue as I said in my comments
I think you can figured it out how do this
First, add a Width and a Height constraints:
Second, change the Equal to the Bigger Than(both for width and height, height could be small one, like 8):
Finally, you are able to change the font size of label freely, the label will became bigger with the bigger font size. And the label size will be (42,8) if you removed text:
One workaround is to use a UITextField instead, set isEnabled to false, and set the placeholder property to " ".
As long as you don't need it's width to ever be shorter than the width of a single space -- UITextFields' sizes are calculated using the placeholder property by default -- then this will always have the height of a non-empty UITextField regardless of whether it contains text or not.

iOS UILabel bounding rectangle not sized correctly

I have a UILabel (as highlighted in yellow), which has the following conditions applied to it.
Label has variable text length
Font set to 40
Minimum font-size set to 20
Number of lines set to 3
Although this looks like a duplicated question I believe it is not. The issue I am having is that when the text exceeds the available 3 line length after being sized-down to 20 points, the UILabel's bounding box is sized incorrectly (i.e. note the extraneous spacing above and below the text).
The end result should be a UILabel without any spacing. Is there are solution to this, while keeping the number of lines set to 3?
That looks like a bug. If you increase your base font size, you will see the space increase. Also, if you inspect the layout at runtime, you will see the content size to be calculated as too big.
My guess is, UILabel takes your original font size (40) to calculate the content size for 3 lines of text and does not take into account that the font size has already been decreased before truncation.
I fiddled with content hugging/compression priorities but could not make it work either.
The only workaround I found was to manually set the font size down to 20. That will get you the frame you want.

UILabel wrapping incorrectly in a UICollectionView

Here I have a UICollectionViewCell, with a UILabel. My labels are character wrapping long words onto a second line. I thought autoshrink would take precedence over line breaks, since "Cappuccino" can easily be autoshrunk onto just 1 line. I even have Word Wrap on.
Why is this label being character wrapped as opposed to autoshrunk with a minimum font size?
EDIT: I'm aware that numberOfLines needs to be > 0, but programmatically determining numberOfLines based on a label's text and bounding rect doesn't consider autoshrink. I can check if the font size with 1 line != my desired font size, and adjust numberOfLines by guessing, but there's no isTruncated property for me to tell whether the line has broken. So the right question is, how can I programmatically account for autoshrink while determining an appropriate numberOfLines?
you must set a number of lignes > 0 when using this config for all UI text containers.

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];
}

Resources