Use the second line of uiLabel if uilabel only have one line - iphone

In iOS project, I am creating an UILabel with 2 lines of text capacity, and i specified that the UILabel can content 2 lines. If the text only is 1 line, how can specify that the text is writing in the second line and the first be empty?
In other words, i would like that the text was writing in the UILabel from bottom to top.
Specifications:
The UILabel must be 2 lines maxium bigger.
If the text only needs 1 line of UILabel, the line which should be write is the second
If the text needs 2 lines, the UILabel should be completed first with the first line and then the second line.(normally. it works)
Thanks.

Use :
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;

Related

Swift UILabel line spacing of break lines

I have a text that comes from multiple sources and ends with \n\n for a line break in UILabel that has lines set to 0 through Storyboard.
For example:
let text = “Some text at the beginning of the paragraph that is this long\n\nSecond type of text\n\nSome longer text that is on later on in the paragraph”
The output is correct:
Some text at the beginning of the paragraph that is this long
Second type of text
Some longer text that is on later on in the paragraph
I have changed the line spacing to slightly increase the gaps between lines but can’t change the height of the empty line. I want the line break to be approximately half the size of a normal line break. I tried after/before paragraph settings but can’t get an empty line to be half the size.
Any idea if this is possible and what is the best way to achieve this through storyboard or programmatically.
----- Edit:
This is what I tried:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.paragraphSpacing = 0.1
paragraphStyle.paragraphSpacingBefore = 0.1
let attrString = NSMutableAttributedString(string: text)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
cell.firstLabel?.attributedText = attrString

Swift - UITextView maxLines is set but can type beyond

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)

how can a label display string in multiple lines

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

how to make content of NSString goes to next line based on \n on iphone

i have a NSString s=#"hi\nhello\n\nwelcome to this world\ni m jhon"
now how to display this content in UIlabel so that \n allow 1 line gap and \n\n allows 2 line gap
still i am getting same value
NSString *s=aDl.content;// aDl.content=hi\nhello\n\nwelcome to this world\ni m jhon
labelb.text=s;// its same without line break :(
kindly help me
Thanks
Set label's height big enough to accommodate all lines in your string. Then set label's numberOfLines property to 0 - so label will draw its text in as much lines as required. New lines symbols (\n) will be handled by label automatically:
NSString *s=#"hi\nhello\n\nwelcome to this world\ni m jhon"
label.frame = ...//big enough height
label.numberOfLines = 0;
label.text = s;

How to wrap a line in label in iphone?

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