Setting the Line Height/ Line Spacing in an NSTextView - objective-c

How would I set the Line Height or Line Spacing in an NSTextView (i.e. how tall each line is, or how much space is between each line)?

leave the answer in case someone need:
NSMutableParagraphStyle * myStyle = [[NSMutableParagraphStyle alloc] init];
[myStyle setLineSpacing:10.0];
[myTextView setDefaultParagraphStyle:myStyle];

Use the - (void)setDefaultParagraphStyle:(NSParagraphStyle *)paragraphStyle method in your NSTextView.
Documentation on NSParagraphStyle
Documentation on NSMutableParagraphStyle
There is a setLineSpacing: method in NSMutableParagraphStyle. There are also methods relating to line height, the methods under "Setting Other Style Information" in the NSMutableParagraphStyle documentation should prove useful.
I think that's what you're looking for.

Related

Mac OSX: How to stroke text in NSTextField?

I've programming an application for the OSX using Objective C. I have an NSTextField that I'm using to display uneditable text. I'm trying to make the font have a thin stroke/outline around it but struggling to do so.
I've tried to use NSTextView instead of NSTextField and implemented an NSAttributedString as so, however the text is not being outlined at all:
NSAttributedString *string = [[NSAttributedString alloc] initWithString:#"Test String" attributes:#{ NSStrokeColorAttributeName : [CIColor blackColor], NSForegroundColorAttributeName : [CIColor blackColor], NSStrokeWidthAttributeName : #-1.0 }];
[quoteText_Label setEditable:YES];
[quoteText_Label insertText: string];
[quoteText_Label setEditable:NO];
Looking at the other question of SO, they're either aimed at iOS, Swift, or are an overkill for the simple implementation I'm attempting to go for: just a simple black stroke around white font.
Thank you
Switch on Rich Text in the XIB and set the attributedStringValue property of the text field. You don't have to do setEditable.
Read the documentation of NSStrokeWidthAttributeName:
NSNumber containing floating point value, as percent of font point size
Default 0, no stroke; positive, stroke alone; negative, stroke and fill (a typical value for outlined text would be 3.0)
A thin black outline around black text is hardly visible.
Values of the color attributes should be NSColor objects. I think, using CIColor is wrong here.

justify UITextView text when text per line is predefined

I have a UITextView with multiple lines. Each text for each line will be taken from database and will be shown on UITextView. Since text per line is predefined, text for a particular line should not be shown on another line. It works fine for me. But TextView is not justified. How can i justify TextView's text without changing text per line. Would you please help me.
You may set the property "textAlignment" of UITextView like this:
textView.textAlignment = NSTextAlignmentJustified;
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:#"your custom string with '\n' between each line"];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentJustified];
[title addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, title.length)];
titleView.attributedText = title;

NSParagraphStyle line spacing ignored

A simple test that is failed: Make a new project with just one subview (UITextView) and put the following in:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineHeightMultiple = 50.f;
paragraphStyle.lineSpacing = 100.f;
paragraphStyle.minimumLineHeight = 200.f;
paragraphStyle.maximumLineHeight = 500.f;
UIFont *font = [UIFont fontWithName:#"AmericanTypewriter" size:24.f];
self.textView.attributedText = [[NSAttributedString alloc] initWithString:
#"This is a test.\n Will I pass?" attributes:
#{NSParagraphStyleAttributeName : paragraphStyle, NSFontAttributeName : font}];
}
Line spacing is the same as if the attribute were not there. Has anything got this to work successfully? I put in ridiculous numbers just to show that it won't change...
This is a bug in NSHTMLWriter which is the private class which UITextView uses to convert attributedText into HTML. Internally it displays this HTML via a UIWebDocumentView. Read more on the inner workings of UITextView in my writeup here: http://www.cocoanetics.com/2012/12/uitextview-caught-with-trousers-down/
The problem comes from an easy to miss speciality in the font CSS shorthand. If you specify a pixel size with the font shorthand then this sets BOTH the font-size as well as the line-height. Since NSHTMLWriter puts the font AFTER the line-height this causes the line-height to be cancelled out by the font size.
See here for my Radar which includes the full analysis of the bug: http://www.cocoanetics.com/2012/12/radar-uitextview-ignores-minimummaximum-line-height-in-attributed-string/
I suggest you file a bug report as well and mention my Radar #12863734.
I don't know if this is enough for your purposes but I could adjust the line spacing by setting the minimum and maximum line height. Furthermore to use a font I put it into the font property of the text view rather than passing it as the value of NSFontAttributeName in the attributes dictionary. (Maybe this part is not (well) documented?)
About your attributes
lineSpacing is calculated from the bottom of the line to the bottom of the upper line and that space is constrained to values between minimumLineHeight and miximumLineHeight. What I am trying to say is that maybe some values in your attributes are cancelling or overriding others.
Also if you need to just adjust the spacing between line you probably don't need to use paragraphStyle.lineHeightMultiple :)
The code
This worked for me:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = 35.f;
paragraphStyle.maximumLineHeight = 35.f;
UIFont *font = [UIFont fontWithName:#"AmericanTypewriter" size:18.f];
NSString *string = #"This is a test.\nWill I pass?\n日本語のもじもあるEnglish\nEnglish y Español";
NSDictionary *attributtes = #{
NSParagraphStyleAttributeName : paragraphStyle,
};
self.textView.font = font;
self.textView.attributedText = [[NSAttributedString alloc] initWithString:string
attributes:attributtes];
Additional Notes
There seems to be a situation with Japanese/Chinesse and maybe other characters mixed with alphabet characters in the same line. It will make that line to have a bigger leading to solve that you need to set up the minimum and maximum line height as I did.
You can see the problem when rendering my example without attributes.
Setting maximumLineHeight seems to resolve this issue for me;
CGFloat fontSize = 22.f;
titleLabel.font = [UIFont boldSystemFontOfSize:fontSize];
NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
paragraphStyle.maximumLineHeight = fontSize/2;
titleLabel.attributedText = [[[NSAttributedString alloc]
initWithString:#"This is a test.\nWill I pass?"
attributes: #{ NSParagraphStyleAttributeName : paragraphStyle,
NSFontAttributeName : titleLabel.font}]
autorelease];
For this particular string you need to set paragraphSpacing instead. What's about lineSpacing, I believe it's just not supported yet on iOS.
As nacho4d answered, in iOS 6 you need to use minimumLineHeight and maximumLineHeight and set font directly in UITextView, not in NSAttributedString as line height in that case will be overridden.
Please note that when you set font in UITextView, the "editable" property of UITextView should be set to YES, in other case attributed text would not be affected.
These issues are present only in iOS 6. In iOS 7 and above everything is ok;
In my case, none of the paragraph styling was working. The fix was to set the attributed text on the label AFTER doing any frame adjustments on the label. :)

NSTextField add line spacing

I use NSTextField not NSTextView to receive the user input, but I need to custom the font and textColor and line spacing. I use the code below, it's ok for font and color but I don't know how to set a line spacing.
[self.titleField setTextColor:textColor];
[self.titleField setFont:bold14];
And I also use a NSAttributedString to solve the problem:
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];
the code above is ok to show a attributed string, but when I delete the string in the textfield and start to input, the words come without any attribute.
How can I input a string in NSTextField with custom font, color and line spacing?
It's best to stay with NSTextField's attribute setting methods instead of an NSAttributedString because then it can send the settings to the field editor. Every text field has an NSTextView (most of the time) "Field Editor"; and the field editor is what is doing the editing.
Your NSAttributedString isn't sticking because you're only telling the textfield to temporarily display that one string. When the field editor pops up the text field (cell) passes on its own attributes like textField.font and textField.textColor but never the NSAttributedString's attributes.
It would be best to use an NSTextView to be able to use -setDefaultParagraphStyle because you're editing multiple lines anyways, from what I see. If you can't, because of performance problems or something else, then:
Subclass NSTextFieldCell, because that's what does all the NSTextField work, and override
- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj
(declared in NSCell) to set up attributes for your field editor the way you want it, so you can send it a line height value through -setDefaultParagraphStyle (and font etc.) yourself. (textObj is the field editor to be set up).

UITextView character wrap but not word wrap

I have a UITextView that I would like to disable word wrap on but keep character wrapping enabled.
Basically I would like to have a long string 200+ characters that still wraps in the textview but prevents word recognition and/or word wrap. Is this possible and if so can someone point me in the right direction?
Thanks!
This is not possible with a stock UITextView, because there is no lineBreakMode property in its public API. If you don't need the text to be editable, you could look into other solutions such as a UILabel or Core Text. Getting this behavior in an editable control is possible, but will probably be a pretty hard slog.
you can try
NSMutableAttributedString *att=[[NSMutableAttributedString alloc]initWithString:str];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[att addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];