Objective C label line spacing? - objective-c

Is there a way to set the distance of two lines within a UILabel?
I tried to do it within Interface Builder but without success.

The code you want will be something like this:
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:#"Sample text"];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:24];
[attrString addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, strLength)];
uiLabel.attributedText = attrString;

You can use NSAttributedString to add spacing between two lines within a UILabel:
NSString *labelText = #"My String";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:20];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
cell.label.attributedText = attributedString ;
OR
If you are using storyboard then you can control line spacing in the storyboard by selecting text type is attributed and add spacing value:

Since iOS 6, Apple added NSAttributedString to UIKit, making it possible to use NSParagraphStyle to change the line spacing.
Alternatively, you can do this via Storyboards using Attributed Text and then clicking the ... symbol. See link below for screenshot.
https://i.stack.imgur.com/aiNfR.png

Related

NSAttributedString with right alignment removes whitespace at the end

I've created a simple chat app where our messages are on the right side (right alignment) and all other messages are on the left side (left alignment). I'm using NSAttributedString because I heavily modify the text with colors etc.
Each message is a UILabel. My problem is that at the end of the message with the right alignment I want to put a whitespace so it looks like this:
"Some example sentence "
and not like this:
"Some example sentece"
and it's removed everytime I put the whitespace there (I also tried with the non-breaking space \u00a0 and I get the same problem (the space is removed)
My code for the right alignment looks like this:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text /*attributes:attrDict*/];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentRight];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
later I add some other attributes with colors etc. (nothing that changes the text itself). The text is always with the whitespace at the end like this: "Some example sentece "
and at the end I do something like this:
self.attributedText = attributedString;
And... my space is removed. How can I prevent my text from removing the whitespace at the end? I need it there.
EDIT:
if (self.textAlignment == NSTextAlignmentRight) {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentRight];
[paragraphStyle setTailIndent:0.1];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
}
This is my code for the tailIndent and what it does looks like this.
I have a message "test" on the right side of the chat (here it's on the left because I don't know how to right align the text :P)
before tailIndent:
test
after tailIndent:
t
So what happens: The text goes from right to left leaving only the last character in this case. And the tailIndent is only 0.1!
I tried a few values myself, and, contrary to the expectation set by the name of the attribute (and the lack of other guidance in the doc), tailIndent must be negative.
Here's the code (the OP's, basically) without the attribute set:
NSString *text = #"Am I indented?";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentRight];
// paragraphStyle.tailIndent = -18.0;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
self.label.attributedText = attributedString;
Uncomment the line setting tailIndent to a negative value, and you get:
EDIT
Any of the controlling params should be represented as objects, like an NSNumber representing the indent:
NSNumber *theIndent = #(-18);
// then, later:
paragraphStyle.tailIndent = [theIndent intValue];
Only objects, like NSNumbers, may be placed in arrays, dictionaries, core data, etc.

NSMutableParagraphStyle remove old NSMutableAttributedString from UITextView

I try to add bullet point into UITextView in the start of the text but when i Add with this code the old attributedString and font and size not saved.
in this code :
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:currentTextViewEdit.text];
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setParagraphSpacing:4];
[paragrahStyle setParagraphSpacingBefore:3];
[paragrahStyle setFirstLineHeadIndent:0.0f]; // First line is the one with bullet point
[paragrahStyle setHeadIndent:10.5f]; // Set the indent for given bullet character and size font
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [currentTextViewEdit.text length])];
currentTextViewEdit.attributedText = attributedString;
currentTextViewEdit.text = [NSString stringWithFormat:#"1.%#",currentTextViewEdit.text];
I try to add a NSMutableParagraphStyle but its remove the old NSMutableAttributedString that text have before.

NSAttributedString with link

I have string with 2 words. I want to color 2 words with different colors say red and green.
Also it the whole string should have clickable link. I tried with following code, the link attribute overrides the previous colors.
NSString *str = #"xxxx yyyyyy";
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:str];
[attStr addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor] range:NSMakeRange(0, 4)];
[attStr addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor] range:NSMakeRange(5, str.length-5)];
NSMutableAttributedString *clickStr = [[NSMutableAttributedString alloc] initWithAttributedString:attStr];
[clickStr addAttribute:NSLinkAttributeName value:#"textLink:link" range:NSMakeRange(0, str.length)];
As far as I know, you can't override the link style in a default attributed string. You'll have to use something like TTTAttributedLabel though it does have some bugs on iOS 8 still.

Is it possible to apply NSAttributedString to CPTTextLayer's Text

I want to display a "custom label" for each index of CPTScatterPlot. CPTTextLayer takes the textcolor, font size, etc from textStyle property. I want to display each character of the string which is displayed in the CPTTextLayer with a different color. I know it is possible using NSAttributedString but when I passed this argument to [[CPTTextLayer alloc] initWithText:attributedStr], my application crashed. Is it possible to apply a NSAttributedString to CPTTextLayer's text?
As of version 1.3 CorePlot does allow creating CPTTextLayer based upon NSAttributedString. Here's example:
NSString *smallTextPart = #"Small part ";
NSString *bigTextPart = #"and the bigger one";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#%#", smallTextPart, bigTextPart]];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue" size:12.0] range:NSMakeRange(0, smallTextPart.length)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Bold" size:18.0] range:NSMakeRange(smallTextPart.length, bigTextPart.length)];
CPTTextLayer *txtLayer = [[CPTTextLayer alloc] initWithAttributedText:str];
It also works properly with multi line CPTTextLayer labels (you can split CPTTextLayer into separate lines using '\n' character).
Cheers.
Core Plot currently does not support attributed text. You can add an enhancement request to the Core Plot issue tracker so that this feature is considered for inclusion in a future version.

Cocoa NSTextField line spacing

I'm struggling with a very simple problem, I've several NSTextField ( I can't use NSTextView right now) and I need to change the line spacing of the displayed text.
What can I do to reduce row height or line spacing? Shrinking the font size isn't an option.
Any help would be really appreciated!
Have a great weekend,
!)
For reference you want to read this description of paragraph styles: Cocoa Paragraph Styles and note that everything in there is additional space added between lines, between paragraphs, before paragraphs, etc. You can set the values in your NSMutableParagraphStyle to zero but no lower.
To further shrink the spacing between lines, use setMaximumLineHeight, thanks to "6 1" for the code (I've add the setMaximumLineHeight):
NSString *title = #"title here";
NSFont *bold14 = [NSFont boldSystemFontOfSize:14.0];
NSColor *textColor = [NSColor redColor];
NSMutableParagraphStyle *textParagraph = [[NSMutableParagraphStyle alloc] init];
[textParagraph setLineSpacing:10.0]; // this sets the space BETWEEN lines to 10points
[textParagraph setMaximumLineHeight:12.0]; this sets the MAXIMUM height of the lines to 12points
NSDictionary *attrDic = [NSDictionary dictionaryWithObjectsAndKeys:bold14, NSFontAttributeName, textColor, NSForegroundColorAttributeName, textParagraph, NSParagraphStyleAttributeName, nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:title attributes:attrDic];
[self.titleField setAllowsEditingTextAttributes:YES];
[self.titleField setAttributedStringValue:attrString];
Swift version of Jason Harrison's excellent Obj-c answer:
let title:String = "title here"
let bold14:NSFont = NSFont.boldSystemFontOfSize(14.0)
let textColor:NSColor = NSColor.redColor()
let textParagraph:NSMutableParagraphStyle = NSMutableParagraphStyle()
textParagraph.lineSpacing = 10.0 /*this sets the space BETWEEN lines to 10points*/
textParagraph.maximumLineHeight = 12.0/*this sets the MAXIMUM height of the lines to 12points*/
let attribs = [NSFontAttributeName:bold14,NSForegroundColorAttributeName:textColor,NSParagraphStyleAttributeName:textParagraph]
let attrString:NSAttributedString = NSAttributedString.init(string: title, attributes: attribs)
textField.attributedStringValue = attrString
you can use NSAttributedString to show the text.
NSFont *bold14 = [NSFont boldSystemFontOfSize:14.0];
NSColor *textColor = [NSColor redColor];
NSMutableParagraphStyle *textParagraph = [[NSMutableParagraphStyle alloc] init];
[textParagraph setLineSpacing:10.0];
NSDictionary *attrDic = [NSDictionary dictionaryWithObjectsAndKeys:bold14, NSFontAttributeName, textColor, NSForegroundColorAttributeName, textParagraph, NSParagraphStyleAttributeName, nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:title attributes:attrDic];
[self.titleField setAllowsEditingTextAttributes:YES];
[self.titleField setAttributedStringValue:attrString];
It's ok to display text not for inputing text. And I only know how to set line spacing.