Two columns of text in UITextView? - iphone

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

Related

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

How can i resize UIView for an amount of UILabels and resize UILabels to fit text?

I have an UIView that it should contains UILabels, and these UILabels are not fixed if an UILabel is nil, i don't display it, and if is not i must resize the UILabel to fit text (they can be 2 ligne) and put it under another UILabel, and after i must resize the UIView container for the amount of this UILabels. this the Screen Shot that i want to do programmatically:
UILabels can fit large amounts of text by automatically making the font smaller (see the minimum font size value in the Interface Builder). Instead of labels, I would use UITextViews. They fit large amounts of text by allowing the user to scroll. To make them hidden, use change the hidden selector attribute of the UITextViews. Because the UITextViews will never change in size, you can easily calculate how much to decrease the height of the UIView container by per UITextView. Hope this helps!
I think you have to calculate the height of the labels (more specifically, the addition from the original height) one by one. Of course you should be able to do this systematically by having all the labels in an array.
You can use [NSString sizeWithFont:] methods (there are several with similar names) to calculate the height.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html
If you want to keep the same font size then you can calculate the frame size for the label at run time depending upon the length of the text..
See here
You need to used following two properties of label-
lineBreakMode
numberOfLine
when you use both of these properties then you will achieve what you want.

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

How to use NSString's sizeWithFont and drawInRect to workout how much of a string to draw

I'm drawing multiple 'pages' of imagery using a CGContext in the iOS. I've used sizeWithFont and drawInRect combinations extensively in my app. What I need to do is split a large chunk of text across multiple pages. I can size it and work out whether or not it needs another page but how do I know where to chop it? Do I have to do an ugly loop to check word by word until I find a string length that perfectly fits the page and then chop the string at that point? Is there a smarter way?
Any ideas?
Thanks.
The NSString additions to UIKit for drawing text, you can pre-determine the exact amount of space required to render a given text for a given font. If splitting the text into pages, you could use this method.
– sizeWithFont:constrainedToSize:lineBreakMode:
Assuming the font and line break mode is known, create a CGSize having the same width as your page, and use a sufficiently number for height. This would be the maximum size that we are constraining the text into.
CGSize maximumSize = CGSizeMake(pageWidth, 999999999);
CGSize expectedSize = [veryLongString sizeWithFont:theFont constrainedToSize:maximumSize lineBreakMode:theLineBreakMode];
expectedSize will tell us the size that the text will take assuming if it could extend vertically infinitely (well close to). To find the number of pages required, just divide the total height by the height of one page.
NSInteger totalPages = ceil(expectedSize.height / heightOfOnePage);
You would also want to adjust the height of one page to make sure that the last line of text doesn't get clipped. For that to happen, the height of the page should be a multiple of the font's line height. Say the initial page height is 300px, and the font-height is 16px, then there will be some clipping as 300/16 = 18.75 which is not a whole number.
NSInteger linesWithoutClipping = floor(initialPageHeight / theFont.lineHeight);
CGFloat optimalPageHeight = linesWithoutClipping * theFont.lineHeight;
Taking the floor value 18 and multiplying with the font line height 16, we get an optimal page height of 288 to ensure there's no clipping.
Note that lineHeight was introduced in iOS 4.0, but you could calculate it yourselves if needed for older versions.
The way I get around this problem is to split by line returns.
NSArray * paragraphs = [text componentsSeparatedByString:#"\n"];
You still have to do all the work to determine page breaks and alike but I have found this the best workaround so far.

Multi-line UITableViewCell using UITableViewCellStyleValue2 style

I'm trying to figure out how to replicate the UITableViewCellStyleValue2 style so that the detail text can be multiple lines - as seen in the 'address' cells in the Contacts app. Like the Contacts app, some of the fields (like street name) are optional; so it would show say 3 lines instead of 4, if the street was not nil.
I'm I missing a trick, or do I have to create a custom cell in IB? How to ensure the text and detail text labels line-up with other UITableViewCellStyleValue2 cells?
Thanks for any tips.
Another round of searching found this:
http://the-lost-beauty.blogspot.com/2009/11/multi-line-uitableviewcell-using.html
Quickly tried it, and it works - just need to set the font size down a bit.
It sounds to me like you'll have to create a custom UITableCell. The only way to ensure the text lines up is to get the margin/text width values correct, which can be done via trial and error, or using a measuring tool such as xScope.
Create a custom cell for you table and place a UILabel and a UITextView inside it. Position the label & text view to match their x,y positions to the other cells you are using in that table. You insert "\n" in the textview's text wherever you want line breaks to occur. You resize the textview height depending on the number of lines in the textview using something like:
CGRect frame = yourTextView.frame;
frame.size.height = yourTextView.contentSize.height;
yourTextView.frame = frame;
return frame.size.height + 20.0; // Pad the cell's height as necessary for your applicaion
I also had the problem that the textLabel and the detailTextLabel had a different position. Solution: For the detailTextLabel use the same height like the textLabel (e.g. 13)