I have a multiple lines UILabel. The text contains slash ("/") character. Usually slash is a not breaking character. But UILabel handles it as a breaking one. How to make it non-breakable?
Example
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 2;
textLabel.text = #"Some text with/slash";
This code is rendered as:
Some text with/
slash
Should be rendered as:
Some text
with/slash
For Slash, use this:
textLabel.text = #"Some text with\/slash";
For avoiding word wrap in label, use this;
[textLabel sizeToFit];
Update:
Below comment suggests that I misunderstood the problem here..
Anyhow, if you want to print it in two lines, use next line carriage ..(/r)
textLabel.text =#"Some text \r with/slash";
Working as you wanted.. :)
Let me know if any info needed.. :)
Related
I followed a that told to use "TruncatingTail" not "WordWrapping" so I used this. But still not able to find the way to get the best optimal output as attached in image.
lbl.text = "1 Year\n=\n365 Opportunities"
lbl.numberOfLines = 0;
lbl.lineBreakMode = .byTruncatingTail;
lbl.adjustsFontSizeToFitWidth = true;
lbl.minimumScaleFactor = 0.1;
lbl.textAlignment = .center;
lbl.backgroundColor = .red
lbl.font = UIFont.init(name:"SourceCodePro-Regular", size: 80)
lbl.numberOfLines = 0;
lbl.lineBreakMode = .byWordWrapping;
I am building an App for Quotes, but I am stuck at this point. I have tried all various ways but not able to succeed. Please anyone can help so it will be a great help to me.
Thank you.
From the UILabel documentation for lineBreakMode:
Specifies what happens when a line is too long for the label’s bounds. Character wrap and word wrap are most commonly applied to multiline labels and determine the position of line breaks between consecutive lines. Select word wrap to place line breaks at word boundaries, or character wrap to insert line breaks in words. Truncate head, middle, and tail are usually applied for single-line labels, and describe the placement of an inserted ellipsis to represent the truncated text. Access this value at runtime with the lineBreakMode property.
Truncating the tail doesn't apply when you allow as many lines as required (numberOfLines = 0). Switch back to using byWordWrapping and your auto-shrink settings should do the rest.
This is the code:
NSString *labelString = #"Username: \n some text";
NSMutableAttributedString *labelAttributedString = [[NSMutableAttributedString alloc]initWithString:labelString];
...
self.smartLabel.attributedText = labelAttributedString;
The label is:
Username: some text
Instead of:
Username:
some text
Edit:
The label has enough space to put the text in 1 line, still I want to insert the breakline (programatically!)
It looks like your label has only one line to display increase your label line to two or more than two as shown in given image.
****Edite**:Since your label is OHAtrributedLabel so you can do the same by code given below**
self.smartLabel.numberOfLines = 0;
self.smartLabel.lineBreakMode = NSLineBreakByWordWrapping
Add this line
self.smartLabel.numberOfLines = 0;
and if needed,
self.smartLabel.lineBreakMode = NSLineBreakByWordWrapping
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.
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);
Hello!
How to get text (string) in my UILabel.
What I mean?
I set a text for my UILabel programmatically. But if my text very large the text don't fit to my label and UILabel cutting off this text.
How I can to check new text with a cutting symbol?
For example:
Source: 'Some Large Text'
Cutting text: 'Some Large T...' (need to get this string)
Thanks for response!
You have to set the UILabels's property linebreakemode and number of lines:
label.numberOfLines = 2;
label.lineBreakMode = UILineBreakModeWordWrap;
aditionally you might need to set the frame correctly.
cheers