Split String at specific line for NSString - iphone

Hello ist there a way to split a String for UITableView at a specific line to put the "rest" (the second part of the data) in an own cell
NSString *data;
CGsize *size = [data sizeOfStringWithFont:[UIFont systemFontOfSize:14] constrainToWidth:280.0];
if the size.height e.g. is greater than 1500 i want to split the string at this line position!
thank you

Use "constrainedToSize" (instead of just to width) and render as much as you can.
If you really want to take exactly the text that would not fit, you're going to have to do essentially a search, adding a word at a time and then doing the size check to see how high you have gotten. You could start out with a rough estimate by doing the whole string constrained to something only one line high with boundless width (say 999999) and then divide up the width into however many rows you are wishing to allow to get a rough starting point for adding/removing words from the string (it will not be exact because of word wrapping).
Fundamentally though it seems wierd to take the leftover text and put it in another cell. Are you really sure you don't simply want to change the height of the cell with the text to allow it to fit the whole thing?

I think Kendall has the right idea, but the constrained sizes should be reversed to get the exact height based on word wrapping. Take a sample CGSize that is the same width as your cell, but with a height larger than the max height you expect. In the sample code below, textSize will contain the height of your string as it would appear in your cell with an unbounded height.
CGSize sz = CGSizeMake (
yourCellWidth,
999999.0f );
CGSize textSize = [yourString sizeWithFont:yourCellfont
constrainedToSize:sz
lineBreakMode:UILineBreakModeWordWrap];
If the height is greater than 1500, you could start picking off substrings (substringWithRange) from the end and measuring them like above until you get something >= the remainder above 1500 that was returned by textSize.

Related

Get the size of wrapped text in Label

If I create a Label in a 500x500 area with wordwrap, how can I find out the height of the wrapped text ? I'm looking for the yellow height, not the salmon height.
Answer of #idrise doesn't work for system font And here I give a more flexible answer.
Assume we want to create a text/label which has a fixed width, but dynamic height according to text's length. for that you can use below code:
Label *lbl = Label::createWithSystemFont("aaa aaa aaa aaa aaa aaa", "Arial", 50);
lbl->setDimensions(FIXED_WIDTH, 0); // "0" means we don't care about wrapping vertically, hence `getContentSize().height` give a dynamic height according to text's length
////
auto dynamicHeight = title->getContentSize().height; // According to text's length :)
And obviously for fixed height you can do similarly.
Hope Help someone :]
This may seem a little counter intuitive.
First you set the dimensions with an excessively large height.
Calling getLineHeight and getStringNumLines will calculate the height based on the width passed.
You send the width and height back to setDimensions.
Now your labels getContentSize() will return the actual size of the text.
IE
label->setDimensions(width, 2000);
label->setDimensions(width,label->getStringNumLines() *
ceil(label->getLineHeight()));
They added the functionality you want:
Added three overflow type to new label: CLAMP, SHRINK, RESIZE_HEIGHT.
Overflow type is used to control label overflow result, In SHRINK mode, the font size will change dynamically to adapt the content size. In CLAMP mode, when label content goes out of the bounding box, it will be clipped, In RESIZE_HEIGHT mode, you can only change the width of label and the height is changed automatically. For example:
//Change the label's Overflow type
label->setOverflow(Label::Overflow::RESIZE_HEIGHT);
mTexto=Label::createWithTTF(mTextoHelp.c_str(),CCGetFont(), 30);
mTexto->setHeight(100.f);
mTexto->setOverflow(Label::Overflow::RESIZE_HEIGHT);
mTexto->setDimensions(mSize.width*0.8f, 0.f);

Relative UILabel in a game object

I created a game object which acts as a repeating item for a UIGrid which I populate dynamically. The gameobject (RowItem) has couple of UILabel whose text can change on runtime depending on the content. The content of these UILabels overlap when the text is bigger. Can anybody help me in how to make UILabel expand relative to the adjacent UILabel when the text is more/less?
You can use transform.localScale property of the UILabel's property to scale it. Just make them bigger when the text is bigger than let's say 20 characters. Try with arbitrary values.
Also when you change the scale, run a re-align method, which aligns other labels so that they don't overlap.
you can get the text length in pixel by this:
UILabel label;
float width = label.relativeSize.x * label.transform.localScale.x;
float height = label.relativeSize.y * label.transform.localScale.y;
Let's say that you want to set you max length to 100, you can do this:
if (width > 100)
{
label.localScale = new Vector3(100 / label.relativeSize.x, 100/ label.relativeSize.x, 1);
}
the second param for Vector3 is also based on relativeSize.x is not a typo, that makes sure your text will not become thin.
Hope this works.

Height of string when unicode text is in the string in iPhone

When I try to use this function having unicode text, it returns wrong height as its calculating height according the font size but emoticons are coming bigger and taking more height than without unicode.
[text sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:CGSizeMake(233.0f, 999.0f) lineBreakMode:UILineBreakModeWordWrap];
can real height be calculated having unicodes in the string?
Have a look at the CoreText framework.
You could try something like:
CTFrameSetterRef framesetter = CTFramesetterCreateWithAttributedString(myAttributedString);
CGSize size = CTFramesetterSuggestFrameSizeWithConstraints(frame setter, CFRangeMake(0, stringLength), NULL, CGSizeMake(maxWidth, maxHeight), NULL);
This should get the correct size for you. You can of course make maxHeight arbitrarily large to make sure that the string fits into the max size. Or you can pass a pointer to a CFRange as the last parameter and it will set it to the range of the string that it was able to fit into the max size that you passed it.
Hope this works for you :)

How to count line breaks (not /n) in an UITextView?

I would like to count how many automatic line breaks (not returns the user enters) I have in a text displayed in UITextView which is, for argument's sake, 200 pixels in width and 460 pixels in length (see attached screen shot!).
I have found this when looking for a solution:
stringSize = [t sizeWithFont:f constrainedToSize:CGSizeMake(320, 10000)
lineBreakMode:UILineBreakModeWordWrap];
But this won't give me an int number for 'invisible' line brakes, will it? Also, I don't understand the 320, 10000 ... 320 is for the width I guess and would need to be changed to 200 in my case. But why 10.000 ??
Sorry, but I'm a beginner and this doesn't make much sense to me...
The CGSizeMake statement is to provide bounds in order to compute the size of text. It is common to constrain the width and to set a high value for the height to let enough space for the computation.
In your case, the width will be 200 and you can leave the height to 10000 as long as the text is not too long.

Wrap text in a UILabel with quotes?

By default, UILabels truncate text and then put an ellipsis on the end.
How might I wrap all the text, including the ellipse, in double quotes?
Use two UILables, the first holds the text (plus an open-quote), and the second just holds a close-quote:
["text that is lon…]["]
UILabel *label;
label.lineBreakMode = UILineBreakModeMiddleTruncation;
Unless there's an even better convenience method on the iPhone that I don't know about, I think the easiest and most flexible thing to do would be to subclass UILabel and implement your own drawing and truncation, using the various sizeWithFont extensions to determine the width of the string and each set of quotes individually.
Is your label text predictably going to result in truncation (and thus always have the ellipse)? I doubt it, but in case it does, you know the content is going to basically fill the width, so you can make the quote marks other UILabels (or even images). This would give you font and color control as well.
There is a correct way to do this, but it won't be the simplest thing ever. You need to do the following:
Determine the max height and max width of your label, with quotes. Determine the actual size of the label. You can use sizeWithFont:constrainedToSize:lineBreakMode: to do this. If the first is smaller than the second, strip the last word from your text, add ellipsis, and try again. That will look something like:
NSString *nextLine = rawTextWithoutQuotes;
NSRange range = [nextLine rangeOfString: #" " options: NSBackwardsSearch];
if (range.location == NSNotFound) {
return nextLine;
} else {
nextLine = [nextLine substringToIndex: range.location];
}
Keep doing this until you have manually truncated your string, then add the quotes and ellipsis, put it in your label, and you're done.