label wrapping when too long - objective-c

I have added Label to my ViewController.xib.
The problem is that when the text of the label is too long, and only part of the text is seen. It would not go to the next line.
How can this be done?

Set the property either programatically with:
#property(nonatomic) NSInteger numberOfLines
or in interface builder.
Here are the docs:
This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.
If you constrain your text using this property, any text that does not fit within the maximum number of lines and inside the bounding rectangle of the label is truncated using the appropriate line break mode.
When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.

Related

Word wrap position in UILabel

When working with a muti-line UILabel object, is there a way to know at which position in the text the line wrapping will occur?
For example if the text of my label is 20 words, the display will be different according to the size of the box provided. I would like to know when (character position) the display is going to the next line.

Resizing a dynamically created label to fit the text inside it?

I'm creating a label dynamically, and then adding text to it dynamically (and the amount of text added to it will be different each time). So, the label needs to always be the same width as the text inside it.
This happens by default when creating a label in the Windows Designer. But when creating a label dynamically, it appears to be "set" at a specific width, regardless of how much text is in it (which means that it often "cuts off" some of the text).
So... any idea how I can get the dynamically created label to always stay the same width as the text inside it?
If you want to do it manually, you can do something like this, every time you change the text:
Dim g As Graphics = Label1.CreateGraphics()
Label1.Width = CInt(g.MeasureString(Label1.Text, Label1.Font).Width)
However, it's much easier to simply set the label's AutoSize property to True and let the label do the work for you.
Label1.AutoSize = True

Moving label to fit according to length vb.net

Hi I have a button which will add a string in to a label. (this can be done multiple times.) As the user can add multiple strings with each one been a different length, is there anyway I can get it to find the length of the label and those around it so that it spaces out correctly?
Thanks
Label.Width will return the current width of the label control, but it sounds like you already know this. Since the label can be narrower than the text it is trying to display, you would need to measure the full text using the graphics object. This method will return the width of the text in a label:
Private Function getFullTextWidth(lbl As Label)
Using g As Graphics = Label1.CreateGraphics()
Return g.MeasureText(Label1.Text, Label1.Font).Width
End Using
End Function
Alternatively, you could just set the label's AutoSize property to true, and then just check the label's Width property after you set the Text.

How to do multiline UILabel in ios?

I'm dynamically populating the title (UILabel). Sometime it's bit too long and IOS squeeze the font to fit in the width. Is there a way to do multiline with using same font size?
Set adjustsFontSizeToFitWidth to NO and numberOfLines to 0.
numberOfLines Docs
This property controls the maximum number of lines to use in order to
fit the label’s text into its bounding rectangle. The default value
for this property is 1. To remove any maximum limit, and use as many
lines as needed, set the value of this property to 0.
If you constrain your text using this property, any text that does not
fit within the maximum number of lines and inside the bounding
rectangle of the label is truncated using the appropriate line break
mode.
When the receiver is resized using the sizeToFit method, resizing
takes into account the value stored in this property. For example, if
this property is set to 3, the sizeToFit method resizes the receiver
so that it is big enough to display three lines of text.
You may additionally want to specify the lineBreakMode unless the default UILineBreakModeWordWrap is suitable for what you want.
Use UILabel properties as below :
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
Yes, you set the numberOfLines to 0, and the .lineBreakMode to UILineBreakModeWordWrap in code, or the equivalents if your label is defined in IB.
I just used LineBreak mode to Truncate Tail and setNumberOfLines to 0 and its working for me.
the label is of multiline now.

How to increase the size of a text box base on the text content

I have a report that I need to increase the size of the textbox base on the length of the text. There some formula to calculate the resulting lenght size?
You can either go for a Label with the AutoSize property set to True. Or if you want to stick to TextBox, you can make use of MeasureString method and get the width of the text from there. Use that width to set the width of TextBox.