UILabel with 2 lines, how to truncate each line independently? - ios

I'm trying to display a UILabel with 2 lines with something like this:
"Here is the first line (a long one) and that's it"
"And this is the second line with random number of chars"
With Truncate Tail it displays this:
"Here is the first line (a long one) and that's ..."
My goal is to display:
"Here is the first line (a long .."
"And this is the second line wit.."
Is there a way to do this with UILabel set to 2 lines and without using 2 UILabel's ?

Split the string on \n to create two strings. Then create 2 UILabels set to numberOfLines = 1 and lineBreakMode = .ByTruncatingTail. Lay them out in the view, one on top of the other.

First, you need to set the numbers of lines to 0(infinite number of lines);
textLabel.numberOfLines = 0;
Next, you can break wherever you want by doing this:
textLabel.text = "str1 \n str2"

Related

Disable Line Wrapping in a multi-line UILabel

Is there a way with a multi-line label (myLabel.numberOfLines = 0) to disable any kind of line wrapping so that if a line is too long to fit on one line of the label it just stops/kind of breaks off and doesnt wrap to the line below? So I can use "\n" to assign strings to other lines of the label. I know lines that are too long automatically wrap to the next line but I dont know if there is a no line wrap option.
So If I had a label with a line max of 10 chars per line
var firstLine : String = "This is 16 chars"
var secondLine : String = "This is too long"
myLabel.text = firstLine + secondLine
// It would look like this:
Output:
This is 16
This is to
As shown it just cuts off and doesnt wrap each line even though they dont fit
firstLine + secondLine will become 1 string This is 16 charsThis is too long, i dont think you can do something like described without code, you have to manually cut off the strings to 10 characters and add \n at the end, so it will become This is 16\nThis is to
Something like :
var string = message
let fontAttributes = [NSFontAttributeName: font]
var size = (string as NSString).sizeWithAttributes(fontAttributes)
while size.width > label.width {
string.removeAtIndex(string.endIndex.predecessor())
size = (string as NSString).sizeWithAttributes(fontAttributes)
}
string = string+"\n"
If you have desired length of your label, you can try this stupid but work method: To cut the string by yourself, use stringByPaddingToLength method.
Try below codes:
self.tempLabel.text = #"This is 16 chars fbaebfbefbefbeif";
self.tempLabel.text = [self.tempLabel.text stringByPaddingToLength:10 withString:#"" startingAtIndex:0];
See magic happens

UITableView blank space not working?

so I was trying to align the text in UITableView using.
cell.textLabel?.text = sometext
I have two part in sometext first part is words, second part is number, such as "apple 45" "pear 23", "banana 34"so when they are showing in tableview cell, I want the left side of the words align with each other and the left side the number align with each other. and I can not post a picture here.
so according to the first part word length I added some blank space in the string by appending
let appStr = String(count: 22-cnt, repeatedValue: ( " " as Character))
print("append string is" + appStr + "end")
nameHere = name + appStr + number
I printed out to console in the program and it works fine, but when showing in the simulator it is not aligned.
Don't attempt to align numbers using spaces. Set the label's attributedText, not its text, and use the fact that an NSAttributedString can have tab stops to perform the alignment.

Adding multiple Lines of text into UILabel in swift

Hi I want to display some text in my UILabel line by line when an action is occurred
Hi, I have a textbox, button and a UILabel, lets say I've typed in
"This text will be displayed line by line in a UILabel"
and pressed the button to display the text in the UILabel..
so inside the UILabel it will look like:
This
text
will
be
displayed
line
by
line
in
a
UILabel
....
I've been searching forums but could not found an answer.. so I don't know if this is possible.. please help
p.s I've set number of lines to 0 and line breaks is set to word wrap
let label = UILabel(frame: CGRect(x: 100, y: 100, width: 100, height: 300)) // make the height biggest
// label.lineBreakMode = NSLineBreakMode.ByWordWrapping
// label.numberOfLines = 0
dont work if label width bigger word width
var string = "This text will be displayed line by line in a UILabel"
var array = string.componentsSeparatedByString(" ")
var newstring = join("\r", array)
label.text = newstring
let labelText = "Write\rthe\rtext\rwith\rline\rbreaks."
In order to wrap your text within a UILabel on Xcode 6 and above
Select the label you are working with.
Navigate to the Utilities panel.
Select the "Attributes inspector"
Select the number of lines you would like to display on the label (2,3,4) depending on the size of your UILabel field
Select "Word Wrap" under Line Breaks, or you can select any option of your preference.
You may manipulate the way your text appears to your taste.
Does it have to be a UILabel?
You could use UITextField instead which has some advantageous handling options

UILabel truncate middle lines in multi-line label

I have a UILabel that has 3 lines max. Here is the code:
_testLabel = [[UILabel alloc] init];
_testLabel.text = #"Line 11111111111111\nLine 2\nLine 3";
_testLabel.numberOfLines = 3;
_testLabel.lineBreakMode = NSLineBreakByTruncatingTail;
[_testLabel sizeToFit];
_testLabel.width -= 5;
By calling sizeToFit and then subtracting 5, I cause line 1 to wrap. This in turn pushes line 3 outside of the allowed bounds, so at the end of "Line 2" there is a "..." and line 3 is not shown.
Instead of wrapping line 1 and truncating line 3, what I really want it to truncate line 1. This way, any lines that are too long to fit in the specified width will be truncated and NOT wrapped.
Is there a way to achieve this?
The best I can think of is to split the string at every '\n' char, and have a separate UILabel for each line with numberOfLines set to 1 for each.
EDIT:
To be more clear, here is what the label looks like with the example code above:
Line
11111111111111
Line 2...
And here is what I would like it to look like:
Line 11111111111...
Line 2
Line 3
Check the enumerators for NSLineBreakMode
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/ApplicationKit/Classes/NSParagraphStyle_Class/Reference/Reference.html#//apple_ref/c/tdef/NSLineBreakMode
You probably want to calculate the size of the UILabel before you set its text, then tweak your line break settings.
See NSString's method:
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context
You are doing it right . You need only one other line .
set "adjustsFontSizeToFitWidth" with "false" in order to prevent the label from fitting width by changing font size.

UILabel lineBreakMode, break at specific character

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.
Sure, just replace all the occurrences of ";" with ";\n" before you show the string.
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.
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"];
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);
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);

Resources