How to add line break for UILabel?
Use \n
as you are using in your string.
Set numberOfLines to 0 to allow for any number of lines.
label.numberOfLines = 0;
Update the label frame to match the size of the text using sizeWithFont:
. If you don't do this your text will be vertically centered or cut off.
UILabel *label; // set frame to largest size you want
...
CGSize labelSize = [label.text sizeWithFont:label.font
constrainedToSize:label.frame.size
lineBreakMode:label.lineBreakMode];
label.frame = CGRectMake(
label.frame.origin.x, label.frame.origin.y,
label.frame.size.width, labelSize.height);
Update : Replacement for deprecated
sizeWithFont:constrainedToSize:lineBreakMode:
Reference, Replacement for deprecated sizeWithFont: in iOS 7?
CGSize labelSize = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];
label.frame = CGRectMake(
label.frame.origin.x, label.frame.origin.y,
label.frame.size.width, labelSize.height);
Insert line break for UILabel text when a specific character is found
You can achieve this with the attributed text property of UILabel. Try to find and replace the character with html line break and then assign this text to the UILabel attributed text.
You can replace string by
let str = "This is the string to be replaced by a new string"
let replacedStr = str.replacingOccurrences(of: "string", with: "str")
How to break line of a UILabel dynamically with text?
Drag a label to your storyboard and add top and leading constraints to it.
Now select the label and control drag to the view holding the label (in your case view of ViewController) you will see the pop up and then select equal width
Now your Label's width is equal to your view's width :) That's not you want you want ur label width to be 70% of your view. So select the equal constraint of label, go to property inspector and change the multiplier to 0.7
Now your label width is 70% of your view!
But you don't want it to be 70% always. It can be at max 70% of screen, so
now change the relationship of constraint from being equal to less than or equal to.
select label and change number of lines to 0.
That's it :) have fun :)
Sample O/P:
When text is short - vs - long:
- - -
EDIT:
Not using a storyboard? Not a problem; write the same constraint programmatically and apply it to label simple enough. If you need help lemme know :)
EDIT:
As you have specified that you want to leave the gap at the beginning of each line in label you can achieve it by using Edge insets
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 0};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
How to make a new line with a UILabel?
Try
if let exitingText = label.text
{
label.text = exitingText + "\n" + (textField.text ?? "")
}
and set
label.numberOflines = 0
Line break in UILabel?
Please check that
UILabel *yourLabel;
yourLabel.numberoflines = 0
or not..
if it is not like this please set it to zero
Multiple lines of text in UILabel
Set the line break mode to word-wrapping and the number of lines to 0
:
// Swift
textLabel.lineBreakMode = .byWordWrapping
textLabel.numberOfLines = 0
// Objective-C
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
// C# (Xamarin.iOS)
textLabel.LineBreakMode = UILineBreakMode.WordWrap;
textLabel.Lines = 0;
Restored old answer (for reference and devs willing to support iOS below 6.0):
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
On the side: both enum values yield to 0
anyway.
Change LineBreak Characters from UILabel
Tested solution
Create text
and customText
empty string
let text = "Lorem, Ipsum, is simply, dummy text of, the printing, and typesetting industry."
var customText = ""
Populate customText
by substituting spaces
with non-breakable spaces \u{00a0}
if previous character is not ,
text.characters.enumerated().forEach { (idx, character) in
let prevChar = text[text.index(text.startIndex, offsetBy: max(0, idx-1))]
if character == " " && prevChar != "," {
customText.append("\u{00a0}")
}
else {
customText.append(character)
}
}
Create your label and assign customText to its text
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 115, height: 5))
label.numberOfLines = 0
label.text = customText
label.sizeToFit()
Related Topics
Dismiss View Controller with Custom Animation
Wkwebview Content Loaded Function Never Get Called
Path to Bundle of iOS Framework
You Are Not Authorised to Use This Service Itunes App Upload Error
Xcode 6 Keeps Renaming My App's Directory in iOS8 Simulator After Each Run
Create a Copy of a Uiview in Swift
Update the Constant Property of a Constraint Programmatically in Swift
Swift Override Instance Variables
Xcode 5.1: Missing Required Architecture Arm64
How to Get Location User with Cllocationmanager in Swift
Raw Value for Enum Case Must Be a Literal
Creating Framework That Requires (Depends On) Another Framework
Swift:How to Change Language Inside App
How to Avoid Adding Multiple Nsnotification Observer