I am new to iphone development .I want to wrap up the text in the label.I want to wrap up to a particular position in the label.Thanks.
theLabel.lineBreakMode = UILineBreakModeWordWrap;
theLabel.numberOfLines = 0;
That'll let it wrap an arbitrary number of lines. If you want to limit them, and have it truncate with a “...”, then set the numberOfLines property to that value.
Set "Line breaks" in Interface Builder ( http://drp.ly/d3K65 ) to "Word Wrap" and increase height of the UILabel.
You can do the same thing (or even more complex) in the code, see UILabel reference http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UILabel_Class/Reference/UILabel.html
Related
I have a UITextView that has maxLines set like this:
textView.textContainer.maximumNumberOfLines = 3;
textView.textContainer.lineBreakMode = .byTruncatingTail
textView.isScrollEnabled = false
However, when typing in the textView, you can press return several times and type beyond 3 lines. Visually, only 3 lines appear, but as you type text is being entered on and on. Is there a way to prevent this?
The maximum number of lines is used to define how many will be visible in the interface. This has nothing to do with the amount of text you type.
If you want to add a "limit" to the number of characters you have to do it programmatically.
There are several other answers related to this on SO.
How to limit characters in UITextView iOS
You can try this
textView.textContainer.maximumNumberOfLines = 3
textView.textContainer.lineBreakMode = .byTruncatingTail
textView.layoutManager.textContainerChangedGeometry(textView.textContainer)
I have a label which displays a string triggered by a button and some int values entered in some textfields...
However, how can I make the label's string to be in multiple lines?
I can see a lot of people have asked this question, but for some reason, when I try to follow the answers it deletes the rest of my sentence after the use of "\n", as I have just tried earlier...
If the label is created in storyboard, go to your storyboard and select it, then in the attribute inspector there is a field 'Lines' with default value of 1. change that 0 and that should work.
Try using the following code
var label:UILabel = UILabel(frame: CGRectMake(10
,100, 300, 40));
label.textAlignment = NSTextAlignment.Center;
label.numberOfLines = 0;
label.font = UIFont.systemFontOfSize(16.0);
label.text = "First label\nsecond line";
Also make sure the height of label is enough to handle the string when presented in multiline else it won't show the next line in label.
To calculate height of label according to String this link can be helpful
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);
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.
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.