How to correctly put labels on one line - iphone

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

Related

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

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

Devide text to UITextViews

I have long text and 2 textViews.
I need to insert this text to 2 textViews(they have no scrolling).
It's like book pages(first page is a one textView, second page is a another textView).
So, question is: how to determine which text length I need insert to first textView?
You can set the complete text to the first textView, calculate the visible text range and set the remaining text to the second textView. An alternative is to manually calculate the range of the text that will be visible in the first textView, however it requires ugly recursion with sizeWithFont that i can't believe is so fast, i'd follow the first way.
See this answer to learn how to get the visible text range.
First figure out length of each line, font, and how many lines you can fill into the each textview. Use following to do this
CGFloat stringWidth = [text sizeWithFont:[UIFont fontWithName:your_textview_font_name size:your_textview_font_size]].width;
Then use substrings operation.
Shouldn't be difficult to do this!

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

How to Calculate Text Size Prior to Creating a CustomCell

I'm creating a CustomCell that contains a UILabel, by default the UILabel will have two lines of text with wrap around enabled, but there are occasions when the text will require three lines.
The font type and size is fixed and cannot be changed, and I trying to identify a way of calculating the length of the NSString/UILabel prior to creating the UITableView/CustomCell so that the cell height can be set correctly. The text that will be displayed will be made up of a number of different words e.g. 'Your name is XXXXX XXXXX and your birthday is..' and the XXXXXXX is the element that is variable.
Hopefully this makes sense, one idea I have considered is creating a method that contains a UILabel that is never displayed and populating it with the required text and then checking if 2 or 3 lines are used, but not sure how to do this.
Is there a more elegant method of achieving this?
There are several UIKit functions for calculating with size of a string with various linebreak modes and fonts. See this doc for details.
In particular sizeWithFont:constrainedToSize:lineBreakMode: may be useful for this.
I believe NSStrings sizeWithFont: constrainedToSize: lineBreakMode: is the method you are looking for. This returns a CGSize which you can get the height of to determine your cell's height (and set the label's frame to the correct size obviously).
Note: you should pass a CGSize parameter in to the constrainedToSize: part that is a larger height than what you intend it to be but the correct maximum width of the label

UITableView cell text is cut off

I am having a problem with the UITableview cell cutting off strings whose characters are more than 12 chars. Any ideas why this would occur? I have not made a custom cell at all. I cannot find any solution to this problem through a Google search. Any ideas?
You should be able to set the label properties to re-size the font based on the label's contents using adjustsFontSizeToFitWidth. This will essentially decrease the font size to make the text fit all on one line.
Your cell likely contains a label, which in turn is set to given bounds. What you need to do is ensure that your label is the same size as your longest string, or bigger than it.