Line spacing doesn't work in OHAttributedLabel - ios

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

Related

Get truncated text from UILabel in Swift [duplicate]

I have a single line UILabel. It has width = screen width and the content now is (the content of UILabel can change)
You have 30 seconds to make an impression during an interview
Currently, my UILabel is truncated tail and the word "duration" is not complete
self.nameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
What I want is I want my UILabel still truncating tail and only display complete word.
Like the image below
Any help or suggestion would be great appreciated.
You can do something like this:
let labelWidth = CGRectGetWidth(label.bounds)
let str = "You will have 30 seconds till you give us a good impression" as NSString
let words = str.componentsSeparatedByString(" ")
var newStr = "" as NSString
for word in words{
let statement = "\(newStr) \(word) ..." as NSString
let size = statement.sizeWithAttributes([NSFontAttributeName:label.font])
if size.width < labelWidth {
newStr = "\(newStr) \(word)"
}
else{
break
}
}
newStr = newStr.stringByAppendingString(" ...")
self.label.text = newStr as String
Idea is: we split words and try check the width while appending from the beginning + the string "..." till we found the a word that will exceed the size, in the case we stop and use this new string
Ideally this is not possible,with default UILabel, when you set lineBreakMode to TruncatingTail, depending on the space required by the letter/word the OS will truncate it, one solution to fix the issue you can use following properties depending on your match.
Minimum Font Scale -- Use this property to specify the smallest multiplier for the current font size that yields an acceptable font size to use when displaying the label’s text. If you specify a value of 0 for this property, the current font size is used as the smallest font size.
Minimum Font Size -- When drawing text that might not fit within the bounding rectangle of the label, you can use this property to prevent the receiver from reducing the font size to the point where it is no longer legible.
i am not sure but try it:
nameLabel.adjustsFontSizeToFitWidth = NO;
nameLabel.lineBreakMode = NSLineBreakByWordWrapping;
OR
If you are using storyboard follow these steps i tried this and it working fine
Open Attribute Inspector
Change Line Breaks to Truncate Tail then
Change AutoShrink to Minimum Font Size
here are my screenshots of label after and before applying these properties
new output

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

UILabel truncate tail and skip not complete word

I have a single line UILabel. It has width = screen width and the content now is (the content of UILabel can change)
You have 30 seconds to make an impression during an interview
Currently, my UILabel is truncated tail and the word "duration" is not complete
self.nameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
What I want is I want my UILabel still truncating tail and only display complete word.
Like the image below
Any help or suggestion would be great appreciated.
You can do something like this:
let labelWidth = CGRectGetWidth(label.bounds)
let str = "You will have 30 seconds till you give us a good impression" as NSString
let words = str.componentsSeparatedByString(" ")
var newStr = "" as NSString
for word in words{
let statement = "\(newStr) \(word) ..." as NSString
let size = statement.sizeWithAttributes([NSFontAttributeName:label.font])
if size.width < labelWidth {
newStr = "\(newStr) \(word)"
}
else{
break
}
}
newStr = newStr.stringByAppendingString(" ...")
self.label.text = newStr as String
Idea is: we split words and try check the width while appending from the beginning + the string "..." till we found the a word that will exceed the size, in the case we stop and use this new string
Ideally this is not possible,with default UILabel, when you set lineBreakMode to TruncatingTail, depending on the space required by the letter/word the OS will truncate it, one solution to fix the issue you can use following properties depending on your match.
Minimum Font Scale -- Use this property to specify the smallest multiplier for the current font size that yields an acceptable font size to use when displaying the label’s text. If you specify a value of 0 for this property, the current font size is used as the smallest font size.
Minimum Font Size -- When drawing text that might not fit within the bounding rectangle of the label, you can use this property to prevent the receiver from reducing the font size to the point where it is no longer legible.
i am not sure but try it:
nameLabel.adjustsFontSizeToFitWidth = NO;
nameLabel.lineBreakMode = NSLineBreakByWordWrapping;
OR
If you are using storyboard follow these steps i tried this and it working fine
Open Attribute Inspector
Change Line Breaks to Truncate Tail then
Change AutoShrink to Minimum Font Size
here are my screenshots of label after and before applying these properties
new output

UILabel word wrap and slashes

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.. :)

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.

Resources