How do I get UILabel multiline and auto-shrink to recognise word length? - swift

My issue is trying to get a UILabel to have multiple lines and auto-shrink together at the same time.
Various articles suggest the following approach:
textLabel.numberOfLines = 0
textLabel.adjustsFontToFitWidth = true
textLabel.lineBreakMode = .byTruncatingTail
This generally works well, except for when a single word is wider than the label, as shown:
All labels have fontSize of 17, together with the properties above. But the third label, which contains the word sesquipedalianism, falls over as a single word is wider than the label frame width.
Anyone know how to get a label to auto shrink for both content and for individual words?

Maybe it's solves you problem
textLabel.lineBreakMode = .byTruncatingTail change to .byWordWrapping

Select Your UILable give UILable constants
THEN
Auto shrink to Minimum Font Size

Related

UILabel not wrapping correctly after text change

I'm working via the IB and have a UILabel that's stretched almost to the end of the layout. I have it set with Lines=2, because the max amount of lines it should take is 2. However if it's only 1 line long, I would like it to have a vertical justification of top.
Label settings in IB:
Lines:2
Line breaks: Word wrap
In code, in the viewDidLoad method I set the text of the UILabel. However as part of the functionality at a point the text must change. This is my code:
[_main_lbl1 setText:[myUI MAIN_TITLE]]; //Always only 1 line
[_main_lblsub1 setText:[myUI SUB_TITLE]]; //May be 1 or 2 lines
[_main_lblsub1 sizeToFit]; //Causes vertical alignment (I believe)
Whenever I change the text and rerun sizeToFit, the text wrapping becomes totally messed up. Instead of reaching almost the end of the UILabel as set up in the IB, in some cases the text will wrap at little more than half the distance, in some cases it doesn't wrap at all.
Image of layout in IB:
Image of resulting label in simulator:
In the first label it seems to be working ok, the second label doesn't wrap at all.
Is there anything I have to do to keep the text wrapping when changing the UILabel text? Anything else I'm missing?
Note: Updated question to include more detail and pics.
Thanks
The issue is that you're using sizeToFit. Which stretches the label out to fit the text. If you need to change the size you can use:
CGSize maxSize = CGSizeMake(320, 9999); // 999 means it can be as tall as you like
CGSize textSize = [label.text sizeWithFont:label.font
constrainedToSize:maxSize];
label.size = textSize;
You shouldn't have to do anything special. It will automatically wrap the text to fit when you change it. Otherwise the text would run out of the text labels bounds (which is not what you want). Your problem is that sizeToFit permanently changes the frame of the label. It makes it as small as possible while still showing the text. You are having it resize its frame to the original text and then you are changing the text so it is no longer sized properly. You should reset the frame back to it's original, change the text, and finally call size to fit again.
In viewDidLoad:
self.originalFrame = self.mainLabelSub1.frame;
Then in viewWillAppear:
self.mainLabel1.frame = self.originalFrame;
self.mainLabel1.text = #"New Text";
Note:
A good way to see the borders of the text label to get an idea for the wrapping potential is to temporary set the background of the label to something like magentaColor that stands out.

How to correctly put labels on one line

I have three UILabel with different font and I want to put then on one line.
The problem is that as the text length of those label is not fixed, I have to change the location of the second and the third label according to the text length of the label before, otherwise the text will be overlapped.
I plan to set the location according to the text length of the label before and I have to recognize the uppercase and the lowercase. I have to consider more if I use other language.
I want to know is there a better method to solve this problem, thanks!
CGSize size = [#"SomeString" sizeWithFont:[UIFont systemFontOfSize:20.0]];
CGFloat width = size.width;
Look into the CoreText Framework. You'll be able to have much more control over your text characteristics than using UILabels.
Developer Reference:
https://developer.apple.com/library/mac/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html
Tutorial:
http://www.raywenderlich.com/4147/how-to-create-a-simple-magazine-app-with-core-text

iPhone: UILabel text does not fit the way i want

I have a UILabel with some text on it, what I want is if the text with the given font does not fit to the label, I want it to be first linebreaked to a second line, and if still does not fit then it should automatically adjust those 2 lines to a smaller font.
I experienced with the IB changing the settings of linebreaks and number of rows, but couldn't get what I want.
Any recommendadtions?
To my knowledge UILabel does not support auto adjusting the font when there is more than one line.
The only way I know of it to iteratively calculate a fitting font size and then to set the appropriate font manually.
Maybe the sizeWithFont: method is a solution for you:
– sizeWithFont:forWidth:lineBreakMode:
This calculates the width / height of a NSString with the appropriate font / settings

UILabel need to setNumberOfLines to fit my text size

I have a UILabel. I need to set the number of lines to fit to it's text.
You can set the numberOfLines property to 0, which removes any line limit and uses as many lines as needed.

Two columns of text in UITextView?

Is it possible to organize text into two columns with just a UITextView? If not, how can I calculate the height for a given string of text?
NSString has metric functions such as
sizeWithFont:forWidth:lineBreakMode:
that you will need to use. Actually I usually use sizeWithFont:constrainedToSize:lineBreakMode:, after creating a CGSizeMake(columnWidth, FLT_MAX) which is then used for the result also - with FLT_MAX as height, only width is being constrained. The resulting CGSize returned will tell you the actual height.
I think that with that in mind it will be much easier for you to use two UITextViews and to break the text into two sections.
A single UITextView can only display one column.
To get the height, create an offscreen UITextView and then try something like:
myTextView.text = theText;
theTextHeight = [myTextView contentSize].height; // in points