Reputation: 11766
Is there a way to break the text in a UILabel
at a specific character say ";" ??
I don't want it to be broken with Word Wrap or character Wrap.
Upvotes: 2
Views: 4103
Reputation: 454
There is another way which will work in limited circumstances. You can replace your normal spaces (\U+0020) with non-breaking spaces (\U+00A0). This will allow you to limit the number of places your string breaks. For example if you had a string like;
I have a string with two sentences. I want it to preserve the sentences.
By carefully using non-breaking spaces you can get it to break like this;
I have a string with two sentences.
I want it to preserve the sentences.
HOW:
For Strings in InterfaceBuilder:
Go to System Preferences -> Keyboard -> Input Sources -> + Select the category 'Others' and the keyboard 'Unicode Hex Input' Select 'Add' Make sure 'Show Input method in menu bar' is selected. CLose System Preferences
Go back to XCode and find your string Using the keyboard menu in your menu bar, select the Unicode keyboard In your string select a space. Type Option+00a0. A space will appear when you've completed the 4 digit sequence. That space is a non-breaking space. Repeat for all spaces you need to.
For programmatic strings you can just add \U00A0 as appropriate.
Upvotes: 3
Reputation: 7363
You can use (\r)
instead of newline (\n)
to create a line break.
Set numberOfLines
to 0
to allow for any number of lines.
yourLabel.numberOfLines = 0;
Like With in your case just replace ;
with ;\n
NSString *string = @"This; is a; NSString";
string = [string stringByReplacingOccurrencesOfString:@";"
withString:@";\n"];
Upvotes: 2
Reputation: 17535
You can't break line using ;
this character. if you want to break line then replace this character with \n
character.
label.text=[label.text stringByReplacingOccurrencesOfString:@";" withString:@"\n"];
And make
label.numberOfLines = 0.
And Update the label frame
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);
Upvotes: 1
Reputation: 10251
You can do with combination of newline character and line break.Check the following code,
self.testLabel.text = @"abc;\nabc;";
self.testLabel.numberOfLines = 0;
CGSize labelSize = [self.testLabel.text sizeWithFont:self.testLabel.font
constrainedToSize:self.testLabel.frame.size
lineBreakMode:self.testLabel.lineBreakMode];
self.testLabel.frame = CGRectMake(
self.testLabel.frame.origin.x, self.testLabel.frame.origin.y,
labelSize.width, labelSize.height);
Upvotes: 0
Reputation: 33421
Sure, just replace all the occurrences of ";" with ";\n" before you show the string.
Upvotes: 4