How to Add Line Break for Uilabel

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

Xcode storyboard screenshot

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

Xcode storyboard screenshot

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.

Xcode storyboard screenshot

select label and change number of lines to 0.

That's it :) have fun :)

Sample O/P:

When text is short - vs - long:

example when text is small - - -
example when text is more

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



Leave a reply



Submit